• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving the top-level JFrame  RSS feed

 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!