Disabling a checkbox in a JTable

Hi all..

I have a column in a JTable that consists of checkboxes. These checkboxes

are created via my getColumnClass method in my table model - I guess a

default cell renderer(?). I also specify what cells are editable and not

editable in my model. This works fine - if a cell is not editable, I cannot

set or remove a 'check' in my checkboxes. But - the renderer paints the a

checkbox component as 'enabled' even though the cell is uneditable. How do I

make the renderer paint a checkbox as disabled if the cell is uneditable?

Anyone?

Thanks!

/Morten

[636 byte] By [mort101] at [2007-9-26 1:16:14]
# 1
Call setValueAt(new Boolean(false), row, column)
alsues at 2007-6-29 0:43:48 > top of Java-index,Archived Forums,Swing...
# 2
Hi Morten..Did you find a solution for your problem.. Can you please let me know...Thanks
akkina9 at 2007-6-29 0:43:48 > top of Java-index,Archived Forums,Swing...
# 3

There are a bunch of internal renderers in JTable for the different data types. However, they don't bother to call JTable.isCellEditable. You'll need to create your own cell renderer class.

P.S. Don't use the last suggestion. That will set the actual value of the cell, not is enabled property.

richardmg at 2007-6-29 0:43:48 > top of Java-index,Archived Forums,Swing...
# 4

I tried creating my own rendering class.. and tried to color the background with GRAY color etc.. it seems to grey it out but.. it shows the CHECK BOX with something like (t.. if it is true..and f..if it is false) instead of (tick mark if its true..or empty if it is false)..

Let me know if you have a solution..

thanks

Mahi

akkina9 at 2007-6-29 0:43:48 > top of Java-index,Archived Forums,Swing...
# 5
Please post your renderer class.
richardmg at 2007-6-29 0:43:48 > top of Java-index,Archived Forums,Swing...
# 6

public class TableRenderer extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(JTable myTable,

Object value, boolean isSelected, boolean hasFocus, int row,

int column) {

super.getTableCellRendererComponent(myTable, value, isSelected,

hasFocus, row, column);

if ([static method call which checks if the checkbox is already checked]) {

setBackground(Color.gray);

}

else {

setBackground(myTable.getBackground());

}

return this;

}

}

akkina9 at 2007-6-29 0:43:48 > top of Java-index,Archived Forums,Swing...
# 7

Of course you're getting odd results. You're basing it off of DefaultTableCellRenderer, which is based off JLabel, which is going to call Object.toString() to determine the value to display. The renderer needs to be based off of a JCheckBox

Try this:

public class BooleanRenderer extends JCheckBox

{

public Component getRenderer()

{

setBackground(table.getBackground());

// etc

setSelected(value == Boolean.TRUE);

setEnabled(table.isCellEditable(row, column));

return this;

}

}

richardmg at 2007-6-29 0:43:48 > top of Java-index,Archived Forums,Swing...