• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How to detect FocusLost on JPanel

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks for pointing out this, have corrected my name.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bharti Poorey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bharti Poorey:
Hi Campbell,

Thanks for pointing out this, have corrected my name.



Thank you.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have tried that first with only panels.. but that didn't work.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its a very simple application having two panels.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Is this defined by some swing property?

JPanel inherits this from JComponent - I don't think there's any specific documentation
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Your buns are mine! But you can have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic