JTable with checkBox

Hi I am new to swing.I am developing one application,in that i need to get a table from database and to display in a frame...this i did...but the problem is i want to add checkbox to first column of every row and if i select(multiple row selection) checkboxes (that is that particular row) i need to add this into database as a seperate table.

plz provide me with a sample code.

thanks in advance

[415 byte] By [Java@2007a] at [2007-11-27 5:40:36]
# 1

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]How to Use Tables[/url] for a working example of using check boxes.

If you want to perform some action when a check box is clicked then you need to add a TableModelListener to the TableModel.

camickra at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for the reply,

I saw the tutorials,but every sample code is for adding checkbox to a manually created table not for automatic generated table from database,which I need.Also selection of many rows and adding to database as a seperate table is my main need...please help me out.

Thanks in advance.....

Java@2007a at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 3

So how is it different?

What about the manually created table is different from the automatically created table?

What is the key to the manually created table that allows it to display check boxes?

> Also selection of many rows and adding to database as a seperate table is my main need

No idea what you mean so I can't help you out.

camickra at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 4
Ok let me explain it clearly...Please help me in selecting many rows at a time from table using check box and when I click a JButton I need to add the selected row into database as a separate table...I couldn't find sample code for this particular requirement...
Java@2007a at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 5

a) read the JTable API for all the getSelected... method. One of the methods returns all the rows that have been selected

b) once you know which rows have been selected then you can use the getValueAt(...) method to get the data from each row.

c) now that you have all the data from the row you can update your database.

camickra at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thats of great use...thank you...

But still I have the problem of selecting many rows with JCheckBox....

That is it should respond to Check and Uncheck state of JCheckBox,

Bacause I couldn't able get the CheckBox itemListener for the checkBox I created in my Jtable.

Finally by JButton action it should get the selected value...

Java@2007a at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hey i think this will help u ...

String[] columnNames = {"ID", "Description"};

List jobList = JobDAO.getInstance().findAll();

Object[][] dataValuesRes = new Object[jobList.size()][3];

j=0;

while(iterator.hasNext())

{

Jobs x = (Jobs)iterator.next();

dataValuesRes[j][1] = x.getId();

dataValuesRes[j][2] = x.getFirstName();

j=j+1;

}

for (int i = 0; i < dataValuesRes.length; i++)

{

JCheckBox b = new JCheckBox();

b.addActionListener(this);

dataValuesRes[0] = b;

}

DefaultTableModel model = new DefaultTableModel(dataValuesRes, columnNames);

table = new JTable(model);

528528a at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 8

Forget my earlier suggestion about using a getSeledtion... method.

> But still I have the problem of selecting many rows with JCheckBox....

> That is it should respond to Check and Uncheck state of JCheckBox,

The tutorial shows you how to display a checkbox in a column.

You don't add any special listener to the check box.

When you click on your button, you use the getValueAt(...) method and loop through all the rows in the table and check to see if the value in the model is Boolean.TRUE. If so then you get the data for the entire row and update the database.

camickra at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 9
Ok i'l try with that thanks for the help...
Java@2007a at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 10

One more doubt...

when i compile this code i got the checkBox but I couldn't able to check or decheck,(i.e.., Tick mark is not coming for selection) what should I add in this code

note: trainerTable is my JTable name

TableColumn includeColumn = trainerTable.getColumnModel().getColumn(0);

includeColumn.setCellRenderer(new TableCellRenderer()

{

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)

{

boolean marked = Boolean.TRUE.equals(value);

JCheckBox checkBox = new JCheckBox();

if (marked)

checkBox.setSelected(true);

return checkBox;

}

});

Thanks in advance

Java@2007a at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 11

Hey just add Cell Renderer and Editor also...

TableColumn col = table.getColumnModel().getColumn(0);

col.setCellRenderer(new CheckBoxRenderer());

col.setCellEditor(new CheckBoxEditor(new JCheckBox()));

public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer

{

private static final long serialVersionUID = 1L;

CheckBoxRenderer()

{

super();

}

public Component getTableCellRendererComponent(JTable table, Object value,

boolean isSelected, boolean hasFocus, int row, int column)

{

if (value == null)

return null;

return (Component) value;

}

}

public class CheckBoxEditor extends DefaultCellEditor implements ItemListener

{

private static final long serialVersionUID = 1L;

private JCheckBox button;

protected Border columnBorder;

public CheckBoxEditor(JCheckBox checkBox)

{

super(checkBox);

}

public Component getTableCellEditorComponent(JTable table, Object value,

boolean isSelected, int row, int column)

{

if (value == null)

return null;

button = (JCheckBox) value;

button.addItemListener(this);

return (Component) value;

}

public Object getCellEditorValue()

{

button.removeItemListener(this);

return button;

}

public void itemStateChanged(ItemEvent e)

{

super.fireEditingStopped();

}

}

528528a at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...
# 12

No. You don't need a custom renderer and you don't need a custom editor. These are already provided by the JTable. Where in the tutorial example did they use a custom renderer or editor to display a checkbox. Read the tutorial....

You simply need to tell the table what type of data you are storing in each column and it will choose the appropriate renderer and editor. This is done by overriding the getColumnClass(...) method of JTable or the TableModel.

camickra at 2007-7-12 15:17:10 > top of Java-index,Desktop,Core GUI APIs...