Your thinking is correct. Within the custom TableCellRenderer, you can actually check which column/row/cell is rendered and subsequently assign a column/row/cell specific formatting.
public static class CustomTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
DefaultTableCellRenderer c = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// center everything in the first column
if (column == 0) {
c.setHorizontalAlignment(JLabel.CENTER);
}
// the background and border of the first cell should be gray
if (column == 0 && row == 0) {
c.setBackground(Color.GRAY);
c.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 5, Color.GRAY));
}
return c;
}
}
Please note that the DefaultTableCellRenderer
is called for each individual cell.
All available formatting functions are well described in the respective documentation: https://docs.oracle.com/javase/10/docs/api/javax/swing/table/DefaultTableCellRenderer.html
CLICK HERE to find out more related problems solutions.