Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

# of existing windows

 
A. Wolf
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering how I could check if any windows exist other than using a counter and incrementing or decrementing the value. I would like to check at the closing of a window if any windows are still visible, and if its the last window, exit.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why "other than using a counter and incrementing or decrementing the value"? If you have your windowClosing() handlers decrement the counter, and increment it in a constructor, then this is fairly easy to do.
Otherwise, there's the static method "getFrames()" in java.awt.Frame, which gives you an array of all Frames (which include JFrames.) You can get the array and check its length. This doesn't include Dialogs or other kinds of Windows, though.
 
A. Wolf
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, getFrames() works.
im wondering, if I was to do the increment/decrement, how would I go about doing that?
I'm guessing I should make a new class to hold an integer?
public class integerholder
{
public int thismanywindows = 0;
}

and later use
integerholder.thismanywindows++;
is that the best solution?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This kind of problem is commonly solved with a static variable in a base class from which all windows are derived. Then you don't need another class just to manage this stuff.
But I've worked on systems where windows registered themselves with a window manager on open and unregistered on close. That's a little more elaborate, but helps the manager know exactly which windows are still open rather than just how many. We had to do things like ask all the windows if they were ok to close before ending some work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic