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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| // déclaration de la colonne
TableColumn colOk = new TableColumn(8, 50);
colOk.setCellRenderer(new ReapproCellRenderer());
colOk.setCellEditor(new ReapproCellEditor());
colOk.setHeaderValue("Réappro");
tcm.addColumn(colOk);
// Renderer
private class ReapproCellRenderer
implements TableCellRenderer {
JCheckBox checkBox = null;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row
, int column) {
//Create a panel and add the check box to it.
JCheckBox checkBox = new JCheckBox();
checkBox.setSelected( ( (Boolean) value).booleanValue());
checkBox.setHorizontalAlignment(JLabel.CENTER);
if (isSelected) {
checkBox.setBackground(new Color(0x99CCFF));
}
else {
checkBox.setBackground(Color.white);
}
return checkBox;
}
}
// Editor
private class ReapproCellEditor
extends AbstractCellEditor
implements TableCellEditor {
JCheckBox field = new JCheckBox();
int currentRow;
int currentColumn;
public Object getCellEditorValue() {
setValueAt(currentRow, currentColumn, new Boolean(field.isSelected()));
return new Boolean(field.isSelected());
}
public Component getTableCellEditorComponent(JTable jTable, Object value,
boolean isSelected,
int rowIndex,
int vColIndex) {
currentRow = rowIndex;
currentColumn = vColIndex;
field.setHorizontalAlignment(JLabel.CENTER);
if (isSelected) {
field.setBackground(new Color(0x99CCFF));
}
else {
field.setBackground(Color.white);
}
field.setSelected(((Boolean) value).booleanValue());
return field;
}
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
int clickCount;
// For single-click activation
clickCount = 1;
return ( (MouseEvent) evt).getClickCount() >= clickCount;
}
return true;
}
} |