I have a JTable component in my GUI which displays psuedocode of an algorithm. I want to highlight the current line of execution by changing the background of a particular cell and then changing the cell beneath and so on.
Right now my code changes the backgrounds on all cells in my JTable as pictured below:

The code I am using to archive this current state is as below:
class CustomRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel d = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if((row == 0) && (column == 0))
d.setBackground(new java.awt.Color(255, 72, 72));
return d;
}
}
I then call jTable2.setDefaultRenderer(String.class, new CustomRenderer()); in my constructor.
I assume that:
- This method is being called on every String type table cell.
- That this would only change the colour of the cell at position (0,0)
How do I fix my code so that only cell (0,0) is coloured?
Add an else clause to your
if:Remember that the same renderer instance is used to paint all the cells.