wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to get parent frame of a JMenuItem (or its ActionListener) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get parent frame of a JMenuItem (or its ActionListener)" Watch "How to get parent frame of a JMenuItem (or its ActionListener)" New topic
Author

How to get parent frame of a JMenuItem (or its ActionListener)

Lance Miller
Greenhorn

Joined: Jun 23, 2008
Posts: 21
I am trying to implement a message dialog to be shown when a JMenuItem is selected. I want the dialog window to appear centered in the Application's window. So I need acess to the ParentFrame so I can do this:



The question is how can I get access to that parent component/frame from the ActionEvent? The ActionEvent's source (e.getSource()) is a JMenuItem. It does not have a RootPane (i.e. getRoot() returns null). When I call menuItem.getParent() I get a JPopupMenu. That JPopupMenu does not have a parent (it's null) nor a root pane. So how can get a referene to the Application's frame so my dialog is centered on it?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

Did you try JComponent#getTopLevelAncestor() ?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

I don't think that's going to help you; that method uses getParent. So if there is no parent for the JPopupMenu, that method will not work.

JPopupMenu has a method called getInvoker(). I think you can try that, and get the top level ancestor of the return value.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Lance Miller
Greenhorn

Joined: Jun 23, 2008
Posts: 21
Originally posted by Maneesh Godbole:
Did you try JComponent#getTopLevelAncestor() ?

I should have mentioned in my original post that I did try that and it returned null. Thanks for the response.

Originally posted by Rob Prime:

JPopupMenu has a method called getInvoker(). I think you can try that, and get the top level ancestor of the return value.


That did it. Thanks Rob! I was looking through the JavaDocs to find anything that returned Containers or Components, but must have missed the getInvoker().

For any future Google finds of this thread, here's what I ended up with:


[ September 09, 2008: Message edited by: Lance Miller ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Something I generally do in my applications is keep an instance of my main window in a static instance class. I usually just call this Application so that I can do things like:

Application.getInstance().getMainFrame()

I use it for all sorts of application wide data that I need so that I don't have to pass objects around as much. It also is clearer (to me) than obscure API methods that have no meaning at face value, even though they do work.


GenRocket - Experts at Building Test Data
Deepak Giri
Ranch Hand

Joined: Feb 12, 2009
Posts: 52
Thank you very much.. helped me


SCJP
Paul Galois
Greenhorn

Joined: Mar 01, 2012
Posts: 1
If there's only one Frame in your app, you can use the static method:

 
wood burning stoves
 
subject: How to get parent frame of a JMenuItem (or its ActionListener)
 
Similar Threads
how to use getParent() ?
Exception Null
StackOverflowError in LinkFollower
Positioning dialogs on screen...
PopupMenu & popupwindow