my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Cannot uncheck JCheckBox in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Cannot uncheck JCheckBox in JTable" Watch "Cannot uncheck JCheckBox in JTable" New topic
Author

Cannot uncheck JCheckBox in JTable

Seema Hanji
Ranch Hand

Joined: Jan 29, 2001
Posts: 37
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
amar senapati
Greenhorn

Joined: Nov 16, 2001
Posts: 4
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


--------------------------

hi,
u can use defaulttable model and work out with these codes...i think it will help u.

final DefaultTableModel dataModel2 = new DefaultTableModel(vecHeader2,0)
{
public Class getColumnClass(int col)
{
return getValueAt(0, col).getClass();
}
public boolean isCellEditable(int row,int col)
{
if(col==0)
return true;
return false;
}
};
jtabSelectedColumn = new JTable(dataModel2);
cheers
amar
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Is there any reason why you need to override the default behavior of a JCheckBox in a JTable? If you really need to, your renderer should only be used for JCheckBox columns and extend JCheckBox and not JLabel. You shouldn't have to have a check to see if it is Boolean. You should set the renderer for that particular column.
Seema Hanji
Ranch Hand

Joined: Jan 29, 2001
Posts: 37

Hi Paul ,
Thanks for your reply !!
I need to use custom renderer as I mentioned before , only some rows can have checkBox in the column not all rows , as some rows have boolean value and other rows have empty string (hence display blank) in that column. Hence I am checking the class of the value in the renderer to return JCheckBox only if the class is Boolean else returning JLabel .
I have put system.out statements in setValueAt() method of table model and getTableCellRenderer() method of Renderer . The setValueAt() method reflects correct value (i.e. for 1st click true and next click false ) , but renderer method shows wrong value (i.e. 1st click true and subsequent clicks also true) , hence the checkBox remains checked.
Any idea why ??
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Here is an example using your renderer that works fine.
I did comment out this line in your renderer
value = table.getValueAt(row, column);
but it worked the same either way.
Seema Hanji
Ranch Hand

Joined: Jan 29, 2001
Posts: 37
Thanks for your help !!
I figured it out . I was not setting value properly in setValueAt() method , hence the valu wasn't stored. I corrected it ... now works fine.
Thanks again .
-Seema
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot uncheck JCheckBox in JTable