Re: JTable - Display Boolean as Check Box - Problem Setting Background colour
The DefaultTableCellRenderer simply extends JLabel, so by just
extending that one it will be hard to get to a checkbox.
Following class, taken from the JTable code and adapted to what you
want, could do the work for you:
class BooleanRenderer extends JCheckBox implements TableCellRenderer {
private static final Border noFocusBorder = new EmptyBorder(1, 1,
1, 1);
public BooleanRenderer() {
super();
setHorizontalAlignment(JLabel.CENTER);
setBorderPainted(true);
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
boolean val = (value != null &&
((Boolean)value).booleanValue());
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
if (val) {
cell.setBackground(Color.cyan);
} else {
setBackground(table.getBackground());
}
}
setSelected(val);
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(noFocusBorder);
}
return this;
}
}
Regards,
Bart