Originally posted by Seema Hanji:
Hi All ,
One column in my JTable is Boolean . But this column should show check box and be editable only in some rows .
I am using Renderer and Editor for this column. Following is the renderer .
<pre>
public class CheckBoxRenderer extends JCheckBox implements
TableCellRenderer {
/**
* Constructor
*/
public CheckBoxRenderer() {
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
value = table.getValueAt(row, column);
if (value instanceof Boolean) {
this.setSelected(((Boolean)value).booleanValue());
this.setHorizontalAlignment(JCheckBox.CENTER);
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else{
setForeground(table.getForeground());
setBackground(table.getBackground());
}
return this;
}
return new JLabel("");
}
}
</pre>
Setting editor & renderer as below....
<pre>
CheckBoxRenderer doneRenderer = new CheckBoxRenderer();
DefaultCellEditor doneEditor = new DefaultCellEditor(checkBox);
table.getColumn(COL_NAME).setCellRenderer(doneRenderer);
table.setDefaultEditor(Boolean.class, doneEditor);
</pre>
In the table model I have implemented
getColumnClass() method to return Boolean.class for this column.
and also implemented setValueAt() to store the value in local array , as getValueAt() gets from this array.
If I click the checkBox in the column , it is checked . When I click it next time , it doesn't uncheck the checkBox. I have put System.out in setValueAt() method, it shows setting correct value everytime I click checkBox , but renderer seems to recieve wrong value . First time , value recieved in renderer is "false" , but in successive clicks , renderer always recieves "true" .
Am I missing something ??
Please Help ....
Thanks in advance .
-SSH