• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
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
Did you try JComponent#getTopLevelAncestor() ?
 
Sheriff
Posts: 22785
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.. helped me
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there's only one Frame in your app, you can use the static method:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic