This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes focus of JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "focus of JPanel" Watch "focus of JPanel" New topic
Author

focus of JPanel

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

By default, a JPanel isn't focusable. Yo may need to setFocusable(true)


luck, db
There are no new questions, but there may be new answers.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Apr 20, 2009
Posts: 804
    
    1

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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Jun 13, 2009
Posts: 2157
    
    7
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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Jun 13, 2009
Posts: 2157
    
    7
The table (and everything else on that tab) will be repainted when the tab is selected.


Good point!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: focus of JPanel
 
Similar Threads
Getting error while trying to access Checkbox object from paint method
Understanding JTable.addColumn()
How to add a JLabel in any leaf of a JTree
counter is not shown in JTextField
JFrame size and location problems