1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
.....
JCheckBox jc = new JCheckBox();
DefaultCellEditor de = new DefaultCellEditor(jc);
TableColumn tc1 = jTableRole.getColumnModel().getColumn(1);
tc1.setCellRenderer(new CheckBoxRenderer());
tc1.setCellEditor(de);
......
class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
CheckBoxRenderer() {
setHorizontalAlignment(JLabel.CENTER);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
//super.setBackground(table.getSelectionBackground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected((value != null && ((Boolean) value).booleanValue()));
return this;
}
} |
Partager