I am trying to implement functionality something similar to yahoo mail. I have a JTable having 5 columns. now i want to add one more column which has checkbox for each row. Then when the user selects multiple checkboxs and clicks on add, i want to select corresponding rows from the table and add in the db.
Till now i am able to add a column to the table, showing checkbox in each row.
now my problem is i am not able to select multiple checkboxes at a time.when i select the second one, firstone is getting deselected. can anyone please tell me what should i do to select multiple checkboxes (select multiple rows). I tried for examples in net..but no help. can anyone please give me a sample example, if they have...
Here is my code i am using to add a checkbox column onto JTable
My CellEditor class ........................
public class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
protected JCheckBox checkBox;;
public CheckBoxCellEditor() { checkBox = new JCheckBox(); checkBox.setHorizontalAlignment(SwingConstants.CENTER); checkBox.setBackground( Color.white);
}
public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) {
Component c =table.getDefaultRenderer(String.class).getTableCellRendererComponent(table, value, isSelected, false, row, column);
if (c != null) { //checkBox.setBackground(c.getBackground()); } }
return checkBox;
}
public Object getCellEditorValue() { return new Boolean(checkBox.isSelected());
}
}
In my Tablemodel I implemented these two methods. public Class getColumnClass(int col) { switch (col) { case 0: return Boolean.class; default: return Object.class; } }
public boolean isCellEditable(int rowIndex, int columnIndex) {
SO AS OF NOW I AM ABLE TO SHOW THE CHECKBOX ON THE JTABLE FIRST COLUMN. NOW I WANT TO ADD FUNTIONALITY SO THAT WHEN I SELECT CHECKBOXES(MULITPLE), I WANT TO SELECT THE CORRESPONDING ROWS. BUT NOW WHEN I CLICK ON CHECKBOX, IT IS GETTING SELECTED, BUT WHEN I CILCK ON SECOND THE FIRST CHECKED CHECKBOX IS AGAIN GETTING DESELETED.
I'm not sure what's wrong with your code. But there is an easier way for a checkbox in JTable. If you have the datatype of that column as Boolean, it will be very straightforward. Java tutorial has a simple example on this, TableDemo.java. Hope this helps.