I try to make my table a selected whole row when you click on a cell (which can be done by disabling the column selection), but I do not want the extra thick border around the specific cell you clicked to be highlighted. I was hoping this would be easy, but apparently this is due to the renderers, so I did a lot of research, and the closest I can do it:
JTable contactTable = new JTable(tableModel); contactTable.setCellSelectionEnabled(true); contactTable.setColumnSelectionAllowed(false); contactTable.setRowSelectionAllowed(false); contactTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
I copied the Renderer from the example and only changed the hasFocus () function to use the colors I wanted. Setting colors in isSelected()
did nothing.
The problem with this code is:
It works in only one column specified by vColIndex at the bottom. Obviously, I want this to apply to all columns, so clicking on a cell in one selects the entire row. I could do a for loop to change it for each cell, but I believe that this is the best way to do this, which immediately changes the cellRenderer columns for EVERYTHING.
setForegroundColor()
works to change the text, but setBackgroundColor()
does nothing for the background of the cells. I would like it to really change the background color, as the property implies.
- Solution for # 2: use
this.setOpaque(true);
before assigning backgroundcolor.
When rendering works, it only works with one cell. How can I make it color all cells in a row?
- Solution for No. 3: I figured it out! Instead of using
hasFocus()
, which affects only one cell, if you enable row selection ( table.setRowSelectionAllowed(true)
), you put the color in the if(isSelected)
. Then the whole row is considered selected and it paints all the cells in!
3 was great, but if someone knows No. 1 or can explain to me why it was designed so that you can only apply the render to one column at a time, that would be very appreciated.
source share