I have a JTable with 3 columns. I’ve set the TableCellRenderer for all the 3 columns like this (maybe not very effective?).
for (int i = 0; i < 3; i++) {
myJTable.getColumnModel().getColumn(i).setCellRenderer(renderer);
}
The getTableCellRendererComponent() returns a Component with a random background color for each row.
How could I change the background to an other random color while the program is running?
One way would be store the current colour for each row within the model. Here’s a simple model that is fixed at 3 columns and 3 rows:
Note that
setRowColourcallsfireTableRowsUpdated; this will cause just that row of the table to be updated.The renderer can get the model from the table:
Changing a row’s colour would be as simple as: