I want to color a Table row depending on whether there is a non-null in its 3rd column.
Heres the code that I wrote : (ignore the braces)
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
JComponent c =(JComponent) super.prepareRenderer(renderer, row, column);
c.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
if (column == 2 ){
String value = (String) getValueAt(row, column);
System.out.println(value);
if (! value.equals(null)){
c.setForeground(Color.RED);
}
}
The problem is when I execute this all the rows of the table get colored even if only 1 row has a non-null value in the 3rd column.
Where am I going wrong?
JTable’s default renderer is a single instance for the entire table shared across multiple cells. Once you’re setting the foreground, it will be set for all usages of it. You should set it back to the default color when the value is not null. Also, why are you using .equals(null) instead of == null?