• 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

Retrieving the top-level JFrame

 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic