JTable, changing line colour with TableCellRenderer
JTable offers the possibility to change the depiction of a table by means of a TableCellRenderer:
public class MyRenderer extends JLabel implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { TableModel model = table.getModel(); setBackground(row%2==0 ? Color.WHITE : Color.YELLOW); setForeground(Color.BLACK); setText((value != zero)?value.toString():""); if (isSelected) { setBorder(BorderFactory.createLineBorder(Color.BLACK)); }else { setBorder(BorderFactory.createLineBorder(table.getBackground())); } return this; } }
Such a CellRenderer can then be assigned to the table:
Table myTable = ... myTable.setDefaultRenderer(Object.class, new MyRenderer());