I need to highlight the color of a selected row in a JTable. I'm using my own CellRenderer for this, and it works, but when i select another row, the previous one still stays highlighted. The idea is to keep in blue color just the selected one, and keep in it's original color the other ones. In adittion i'm making the pair columns: gray and the non pair: white, so this is the code at the CellRenderer
private class Renderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
int columnIndex = table.getSelectedColumn();
int rowIndex = table.getSelectedRow();
if (columnIndex != -1 && rowIndex != -1){
this.setBackground(Color.BLUE);
} else {
if (row % 2 == 0) this.setBackground(Color.decode("0xF9F9F9"));
else this.setBackground(Color.decode("0xF1F1F1"));
}
return this;
}
}
EDIT: F1F1F1 is a color nearly to white and F9F9F9F9 is kinda a light gray