• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

focus of JPanel

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want JPanel to be notified when it gains focus. I have the following code:

But just the window gains focus and JPanel is not notified. Should I register JPanel as JFrame windowFocusListener?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, a JPanel isn't focusable. Yo may need to setFocusable(true)
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl. It work in this simple case.

But I have much more complicated situation. There are a lot of JPanels (one inside another). I recon that I have to turn the focus on on every JPanel in order to get the focus on the deepest one?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's more likely that you need to sit back and think about your design (and I'm not talking about how you lay out your GUI). There may be a better approach than using a FocusListener.

What are you doing with the notification anyways?
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have JTable which is placed in the JPanel. That JTable has its own cell renderer. The color scheme for the table is set via GUI and I want to refresh JTable so that it preserves the user's choice.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I'm having a hard time trying to understand
Lucas Smith wrote:The color scheme for the table is set via GUI

Could you describe that in more detail?

To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Note: the actual problem, not "I want JPanel to be notified when it gains focus."

if you're using the MetalLookAndFeel, the color schemes would normally be changed by calling setCurrentTheme(...). For any LookAndFeel that doesn't provide Theme support a series of put(...) calls to the UIManager may be necessary. Or the LaF may provide some other mechanism for changing default colors. A custom renderer shouldn't figure in applying custom colors for the entire table.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to change many features of the table. Some of the cells can be gray and another can be white. Moreover the focus color can be different. This is all configurable via JMenu. It depends on the data in the cells.
After changing color scheme the JTable should take the new color and repaint.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm even more confused. Where does detecting a focus change on a panel come into it?

For the scenario you describe, I would have the custom renderer class contain a field (array or List) or individual fields for the customized colors, set their new values and call repaint() on the table, all in the ActionListener of the menu.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but I have a lot of JTables on many JTabbedPanes. I think that particular table should repaint and change colors when I see it. Why should I paint something invisible to the eye?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to try to do the work of the RepaintManager yourself. And in general, don't micro-optimize, and don't optimize prematurely.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but I have a lot of JTables on many JTabbedPanes.


So your real question is about knowing when a different tab is selected.

You can use a ChangeListener. Then you can just invoked repaint() on the component added to the tab.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even that shouldn't be necessary. The table (and everything else on that tab) will be repainted when the tab is selected.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The table (and everything else on that tab) will be repainted when the tab is selected.


Good point!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic