Meaningless Drivel is fun!
The moose likes Java in General and the fly likes # of existing windows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "# of existing windows" Watch "# of existing windows" New topic

# of existing windows

A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
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

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
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
is that the best solution?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: # of existing windows
jQuery in Action, 3rd edition