I've got a JTable called chances_T full of doubles ranging from 0 to 100.
I'm trying to set the background color of each cell based on the cell's value.
So I built a custom cell renderer:
class CustomRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
double chance = Double.parseDouble(String.valueOf(value));
if (chance > 80) {
c.setBackground(Color.green);
console_TA.append("Worked!\n");
} else if (chance >= 50 && chance < 80) {
c.setBackground(Color.yellow);
} else if (chance >= 25 && chance < 50) {
c.setBackground(Color.orange);
} else if (chance >= 10 && chance < 25) {
c.setBackground(Color.red);
} else if (chance > 10) {
c.setBackground(Color.gray);
}
return c;
}
}
I initialized this renderer after I seeded the table with values (the seeding method works correctly):
CustomRenderer CR = new CustomRenderer();
for (int i = 1; i < 5; i++) {
for (int j = 0; j < 9; j++){
CR.getTableCellRendererComponent(this.chances_T, data[i][j], false, true, i, j);
}
}
It goes through the whole thing, it shows the values, but the cells' background colors remain unchanged. It even appends "Worked!" into the JTextArea called console_TA.
I'll continue experimenting on this!