Swing JTable - Highlight selected cell in a different color from rest of the selected row?
Asked Answered
R

3

8

I have a basic swing JTable and the requirement is that when clicked on any cell, the entire row should be highlighted, and also that the cell which was clicked should be a different color from the rest of the highlighted row.

Currently, I have isRowSelectionAllowed as true

I tried using a custom TableCellRenderer which is as follows:

public class CustomTableCellRenderer extends DefaultTableCellRenderer
{

public static final DefaultTableCellRenderer    DEFAULT_RENDERER    = new DefaultTableCellRenderer();
    @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    if (isSelected) {
        c.setBackground(Color.red);
    }
    else {
        c.setForeground(Color.black);
        c.setBackground(Color.white);
    }
    return c;   
  }     
}

But that did not seem to work (entire row was highlighted in red).

I also tried setting the UIManager property as follows:

UIManager.put("Table.focusCellBackground", 
         new javax.swing.plaf.ColorUIResource (Color.red));

But that does not seem to work either (even though, when I tried setting a border using

UIManager.put("Table.focusCellHighlightBorder", 
         new BorderUIResource.LineBorderUIResource(Color.red)); 

that worked well)

Could you please give any suggestions what I might need to do?

Reinwald answered 28/7, 2011 at 16:8 Comment(0)
M
11

Try this:

jtable.setCellSelectionEnabled(true);

Then in the getTableCellRendererComponent

if (table.isCellSelected(row, column))
    setForeground(Color.red);
else if (table.isRowSelected(row))
    setForeground(Color.green);
else if (table.isColumnSelected(column))
    setForeground(Color.blue);
else
    setForeground(Color.black);

That will render the selected cell in red, the rest of the row in green, and the rest of the column in blue. Note: cell selection requires the selection model be single, other selection models may cause unpredictable behaviors.

Mclean answered 29/7, 2011 at 3:27 Comment(2)
I tried this, but it does not change the color at all. It does go into the if condition for isCellSelected and isRowSelected, but doesnt seem to be doing anything.Reinwald
Sorry, my mistake - I tried, and this works. Thanks a lot. (Setting this as the accepted answer because of working code. Other answers were helpful for learning too).Reinwald
S
4

But that did not seem to work (entire row was highlighted in red).

You need to check the "hasFocus" variable, not the "isSelected" variable.

Another option instead of creating mulutiple custom renderers (in case you table has columns of different class types) is to use the Table Row Renderering approach.

Slur answered 28/7, 2011 at 16:23 Comment(0)
A
3

You would need to turn row selection off and cell selection on for the table. Then find a way to go back and highlight the row if needed.

Acrodont answered 28/7, 2011 at 16:14 Comment(2)
Do you mean I turn on CellSelectionEnabled(true) and then handle a row selection using somethinglike this? table.changeSelection(row, col, toggle, extend); ? It kind of is giving the same result.Reinwald
Yes, to the first part, no to the second. You will need a Cell Renderer that highlights the selected row in a different color than the selected cell.Acrodont

© 2022 - 2024 — McMap. All rights reserved.