File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Retrieving the top-level JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Retrieving the top-level JFrame " Watch "Retrieving the top-level JFrame " New topic
Author

Retrieving the top-level JFrame

Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
How the heck do I do this??? It's driving me nuts and now I'm all cranky and doped up on Mountain Dew.
I know that if I implemented my actions as inner-classes, I could just JMyRealFrame.this and do whatever the heck I want. But I don't want to have to maintain some silly java source file with one JFrame and 50 actions. How horrible would that be!
I would just feel a whole lot better about myself and the world if I could de-couple my actions from my JFrame. Isn't that how the whole MVC thing is supposed to work? Or do people magically forget about it when they look at the clock and see that they've been searching for one silly thing for the last 4 hours?
OR, am I just going about this whole thing the wrong way? Argh! Woe is me ...


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you have a component, you can call SwingUtilities.getWindowAncestor( component ) to get the top-level Window that component is in.

From anywhere you can call Frame.getFrames(), which returns an array of all frames in the application (i.e. all top-level frames in the current JVM).


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Nice name Nathan --
Thanks for the tips -- I never found the Frame.getFrames method last night. I tried the SwingUtilities methods but for some reason (at least when my JMenuItem triggered the action), I couldn't get a JFrame ancestor from the event source. Apparently it stops at JPopupMenu -- which is (I'm guessing) a top-level window these days??
I'm thinking in the end I might as well just pass the reference to my JFrame to the constructor of my Actions. I think that will work just fine, and my OOP friends won't make fun of me for it -- so yeah.
Thanks again.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Retrieving the top-level JFrame
 
Similar Threads
is experience of open source useful?
Getting audio to play in the background of my GUI.
Sizing a JFrame during printAll()
Why Java run more slowly
How do you use javax.swing.Popup?