In my table model, I return Boolean.class
in getColumnClass
so that checkboxes are displayed in the first column. Is it possible to add checkboxes to all cells in the first column except the last cell (next to Total)? See screenshot. Thanks!
Of course, you just need to implement a TableCellRenderer
that returns null when rendering the last row && first column like this one:
public class CustomTableCellRenderer implements TableCellRenderer {
private final TableCellRenderer decorate;
public CustomTableCellRenderer(final TableCellRenderer decorate) {
super();
this.decorate = decorate;
}
public Component getTableCellRendererComponent(final JTable table, final Object value,
final boolean isSelected, final boolean hasFocus, final int row, final int column) {
if (column == 0 && row == table.getRowCount() - 1) {
return null;
}
return decorate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
now you need to set the CellRenderer of the Boolean cell with an instance of this implementation like:
TableColumn column = table.getColumnModel().getColumn(0);
column.setCellRenderer(new CustomTableCellRenderer(table.getDefaultRenderer(Boolean.class)));
With this implementation the last row/ first column will be presented empty.