• Post Reply Bookmark Topic Watch Topic
  • New Topic

Highlight cell in JTable, even once table has lost focus

 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Kinda glad I found a forum other than the one on Sun

Basically, I'm writing a little date-picker widget for part of an app I'm building:



When you click a cell in the table it goes green, as shown above. When you click a different cell it goes back to its default color as expected and the new cell goes green. However, if you click away from the entire table and onto something like a JButton all the cells just go back to their default colors, when what I'd like to happen is have the currently selected cell stay green (so it's visible while the user does other things).

Here's the renderer I wrote to do it. I can see *why* it doesn't work, however, I can't see an obvious solution right now



I tried storing the row and column numbers only when the cell is focused and the comparing to the current row and column number but that seemed to make previously selected cells highlight at the same time

Cheers,

Chris
[ January 14, 2007: Message edited by: Chris Corbyn ]
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't rely solely on the isFocused parameter, because it will always be false when the table is not the focused component.

One way around this is to remember which table cell was last focused. This is essentially Mr. Dunn's approach, and it works fine.

Another way is to ignore the isFocused parameter and instead set the green background to the table cell that has the selection lead. You could do this in your renderer by replacing
this.isFocused = isFocused;
with something like
int leadRow = tbl.getSelectionModel().getLeadSelectionIndex();
int leadCol = tbl.getColumnModel().getSelectionModel().getLeadSelectionIndex();
this.isFocused = (row==leadRow && col==leadCol);


btw, why do you put some of your renderer logic into the setValue() method? It seems to me it would be much easier put everything into getTableCellRendererComponent(). You wouldn't need any those protected fields, for instance.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:
One way around this is to remember which table cell was last focused. This is essentially Mr. Dunn's approach, and it works fine.


Well I don't know if the code I attributed to Mr. Dunn was a hallucination or not, but either way it's not here to examine. So to apply the approach to your render, you would add something like this
if (isFocused) { focusRow = row; focusCol = col; }
else if (row==focusRow && col==focusCol) isFocused=true;

just before the part of your renderer that does this
//Set a special highlight color if this actual cell is focused
if (this.isFocused) this.setBackground(new Color((float)0.5, (float)0.80, (float)0.6));


You also have to add the focusRow and focusCol fields.

This does work, but things can go wrong if you want to use the same renderer instance for more than one table. I don't generally like my renderers to maintain state, so I guess I prefer the other approach.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Well I don't know if the code I attributed to Mr. Dunn was a hallucination or not,

it was there, but once I'd seen the question answered (some hours?) earlier
on the sun site, I deleted my post because the time I'd spent on putting
together the demo program was a total waste of my time.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't see Michael's code posting and did not find this topic at Sun.
So I'll post this as a possible solution/suggestion.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's the post/solution at sun's forum

http://forum.java.sun.com/thread.jspa?threadID=5124682
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!