3

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

2 Answers2

5

You should add something like the code below before you return from the method:

if (!isSelected) {
  setBackground(...);
}
Dan D.
  • 32,246
  • 5
  • 63
  • 79
2

If you do not mind using 3th party libs: the JXTable of the SwingX project has built-in support for alternating row colors using HighLighters.

I found an article which shows some screenshots of the result of applying a HighLighter to the JXTable. Problem is that the article is not up-to-date. The functionality is still there but the code has changed. While the article still mentions the AlternateRowHighligher as shown here

HighlighterPipeline highlighters = new HighlighterPipeline();
highlighters.addHighlighter(new AlternateRowHighlighter());
table.setHighlighters(highlighters);

the current approach would be more like

JXTable table;
Highlighter alternateStriping = 
  HighlighterFactory.createAlternateStriping( Color.decode( "0xF9F9F9" ), 
                                              Color.decode( "0xF1F1F1" ) );
table.setHighlighters( alternateStriping );
Robin
  • 36,233
  • 5
  • 47
  • 99
  • If i don't get an alternative ill get in this –  Oct 02 '12 at 22:52
  • @user1715408 The answer of Dan should allow you to fix your issue. I just wanted to show an alternative approach, and it was a bit long to include in a comment – Robin Oct 03 '12 at 05:57
  • I don't find a way to know when a row is selected... getSelectedRow() just return the index of the selected row... –  Oct 04 '12 at 05:39
  • @user1715408 the selected state of the row is passed as a parameter in the `getTableCellRenderer` method – Robin Oct 04 '12 at 05:53