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 How to detect FocusLost on 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 "How to detect FocusLost on JPanel" Watch "How to detect FocusLost on JPanel" New topic
Author

How to detect FocusLost on JPanel

Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
Hi,

I am creating an application which has a JTree and each treenode has a associated JPanel.

I want to perform certain actions when the JPanel losses focus(when another treenode got clicked), I tried it with creating small program but the JPanel is not responding to either FocusGain or FocusLost.

Any suggestion/idea..How to detect FocusLost on JPanel?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
"bharti" you have obviously missed our naming policy; we require you display first name-space-last name which you are not doing.
Please go to "my profile" and correct the displayed name to match.

CR
Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
Hi Campbell,

Thanks for pointing out this, have corrected my name.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

I dont think having a focus listener to the renderer panel is that good an idea. Have you tried out having a selection listener to the tree?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
Hi Maneesh, thanks for your reply.

The structure is.. in a frame I have a Jtree on the left side, and on clicking on the tree node, the corrosponding panel is displayed on the right side where user can provide inputs. There is one more panel at the bottom of frame which has some buttons.

Here I can not use Selection listener to the tree as I want to save the panel changes either on clicking on the tree node or when the user clicks on the buttons provided at the bottom inside a ButtonPanel.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Originally posted by bharti Poorey:
Hi Campbell,

Thanks for pointing out this, have corrected my name.


Thank you.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

Originally posted by Bharti Poorey:

Here I can not use Selection listener to the tree as I want to save the panel changes either on clicking on the tree node or when the user clicks on the buttons provided at the bottom inside a ButtonPanel.


The button part is easy. I am sure you already have an action listener which saves the changes.
Instead of selection listener, you can use a focus listener to the tree. Whenever the tree gains focus, you can figure out of there is any unsaved changes and take action accordingly.
Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
Yes.. definitely I can use the FocusGain event for tree and other panel.

But the drawback with this approach is, if in future I add any other panel/component to the frame, I need to perform the same FocusGain check for that also.

Is it not possible that on focuslost of panel, it save the data irrespective of who ever gained the focus??
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

You dont need a focus listener to the panel. Only the tree.
The panel in itself is of no use for focus or for that matter any kind of listeners. The data is inside some other components such as a JTextField which is embedded out in the panel.

So the easiest way is to add a focus listener to the tree. As soon as it gets focus you can check for unsaved data and take action accordingly. This way, in future, you don't need to worry about any extra added panels.
Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
Yep.. this will work for my applicatio. Thanks Maneesh.

Just a query.. whether JPanel respond to focus events?
As I tried with a small application to check the focus gain and focus lost, but it didn't work.

Is it mean that the Focus events are not implemented for JPanel in the same way they are implemented for JButton and JTextField component??
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

The JPanel actually inherits the focus listener implementation from the java.awt.Component. So as such there is no separate implementation for the JPanel class.

In your test, did the panel have any children or was it empty?
Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
It has a JButton component.

There are two JPanels on a frame,
Panel1 having Button1 as its child.
Panel2 having Button2 as its child.

I tried to print some statements on focus events like
"Panel1 gain Focus" and "Panel1 lost Focus"
For button :
"Button1 gain Focus" and "Button1 lost focus"

When I switch focus from Button1(Which is in Panel1) to Button2( of Panel2)
I expect it will print :
Panel1 lost Focus
Button1 lost focus

But it prints:
Button1 lost focus

Does it mean only JButton responded to FocusLost , not the JPanel ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

The panel never has the focus in the first place! Its always on the buttons.
Take out all the buttons. Retain the panels and run your code.
Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
Have tried that first with only panels.. but that didn't work.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

Originally posted by Bharti Poorey:
Have tried that first with only panels.. but that didn't work.


Can you please post that panel code so we can take a look?
Dont forget to use the CODE tags
Bharti Poorey
Greenhorn

Joined: Sep 09, 2008
Posts: 9
Its a very simple application having two panels.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
default setting for a JPanel is 'not focusable'

add these 2 lines (then start pressing the 'tab' key, to change focus)
jPanel1.setFocusable(true);
jPanel2.setFocusable(true);
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

Originally posted by Michael Dunn:
default setting for a JPanel is 'not focusable'


I personally was not aware of this. Is this defined by some swing property? I tried looking for its implementation in the source code but was unable to find it. Could you please point me in the right direction?
Thank you.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Is this defined by some swing property?

JPanel inherits this from JComponent - I don't think there's any specific documentation
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

Originally posted by Michael Dunn:
> Is this defined by some swing property?

JPanel inherits this from JComponent - I don't think there's any specific documentation


I am a bit confused here.
JPanel and say a JButton inherit JComponent. So what makes a JButton focusable but not a panel? I mean logically it makes sense. But I am unable to find it in the code.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
somewhere (can't recall) there's a snippet that states once a component has
a keyBinding, it's focusability is set to true.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

Originally posted by Michael Dunn:
somewhere (can't recall) there's a snippet that states once a component has
a keyBinding, it's focusability is set to true.


Ok. Thanks. Ill try and hunt for it.
 
jQuery in Action, 2nd edition
 
subject: How to detect FocusLost on JPanel
 
Similar Threads
JTree CellEditor with Combobox and Textfield
Setting JPanel Focus
Issues on focus with composites and their child widgets
focusListener wont work on JComboBox
Focuslost listener for TableViewer