I've been trying to color a complete JTable row with a custom TableCellRenderer but for some reason it's only coloring my first and last cell.
I want to color based on the second and fourth value, in this case: 5 and 10. If the second value is less then the fourth, color it red. If it's equal color it yellow.
As you can see below it works out fine, but for some reason it only colors the first and last cell and I've been banging my head against the wall trying to get this to work.
I assume I'm doing something wrong in my overriden getTableCellRendererComponent:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
Integer minStock = (Integer) table.getModel().getValueAt(row, 3);
Integer currentStock = (Integer) table.getModel().getValueAt(row, 1);
if (Objects.equals(minStock, currentStock)) {
setBackground(Color.YELLOW);
} else if (minStock > currentStock) {
setBackground(Color.RED);
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
If anyone could enlighten me on how to solve this it would be greatly appreciated!