• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TableCellRenderer setBackground of selected cell not working

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have this JTable...



Columns 0, 1 and 6 are made with custom renderers and column 6 has a custom editor too. Now the problem with this table, is that when the user selects an item in the last column, there should be the same selected border and background color, but instead is it just white. The table is also set to one click activation of the editor.



Code for the renderer of column 6



Code for the editor of column 6



If anyone could help me out with this, it would be greatly apprecitated!

Many kind thanks,
Rachel

[ October 26, 2005: Message edited by: Rachel Swailes ]
[ October 26, 2005: Message edited by: Rachel Swailes ]
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Saloon is great because you get help from people but also because it forces you to write the problem down which help you think of the solution by yuorself anyway.

I got the answer, but I thought I'd write it up here incase anyone else has it too.

Turns out that in the editor for the last column, you don't need to check the state of the selected-ness of the cell, because it has to be selected to click on it. So then just set the color of the cell regardless to

.setBackground((Color)UIManager.get("Table.selectionBackground"));

and it works like a charm!

Thanks anyway...
Rachel
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had that same problem just the other day. It seems to make sense
in an Editor to do

if (isSelected) setBackground(someColor);
else setBackground(otherColor);

but the problem is that if the cell wasn't selected before the user
clicks on it to invoke the Editor, the Editor sees isSelected==false.
So the solution is to always use the selected color since an Editor's
cell is always active. (To state the obvious for posterity, this
applies only to Editors, not to Renderers.)

It does make you wonder, though, why the getTableCellEditorComponent()
method even has an isSelected parameter.



While I'm here, I wonder if anyone would like to comment on the benefits
of using

(Color)UIManager.get("Table.selectionBackground")

vs.

table.getSelectionBackground()

I have always used the latter.
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


btw, you may want to rewrite your Editor to have one JLabel and
one JCheckBox that it reuses over and over. especially if your
table has a lot of rows.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is a definite plan to use one JLabel and on JCheckbox across all the rows. Yesterday I was just hacking around to get the colors right.

As far as the UIManager.get vs the table.getSelectionBackground goes, I used it simply because I didn't know about the table.getSelectedBackground method. As for which is better, perhaps there is someone who can comment on it.

Cheers!
Rachel
[ October 26, 2005: Message edited by: Rachel Swailes ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why you're using a custom editor on the last one. You realize that JTable comes with functionality to use a check box if it's a boolean right?
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
Not sure why you're using a custom editor on the last one. You realize that JTable comes with functionality to use a check box if it's a boolean right?


I don't mean to speak for Rachel, but I see two reasons:
  • Sometimes she wants no box at all. See the third row of her images.

  • The type of the column seems to be SelectableComponent, not Boolean.
  •  
    Ken Blair
    Ranch Hand
    Posts: 1078
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah, was just a quick glance. That would explain it.
     
    Rachel Swailes
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes Brian, that's right. I thought about putting all checkboxes in but making some not accept clicks but that's not really easy for the user to understand. Now if they can't click on a row, there's no box there for them to try click on.

    Thanks again!
    Rachel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic