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]

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.
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
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;
}
}
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;
}
}