Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Lance Miller
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try JComponent#getTopLevelAncestor() ?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lance Miller
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Giri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.. helped me
 
Paul Galois
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there's only one Frame in your app, you can use the static method:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic