3

Or more exact:

I need to know on a window close event, if any other window is still visible.

If not, System.exit(0) would be called.

Mat
  • 202,337
  • 40
  • 393
  • 406

4 Answers4

4

1) I need to know on a window close event

there are WindowConstants and WindowEvent

2) if any other window is still visible.

you can get number of Top-Level Containers by using Window[] wins = Window.getWindows(); for testing their visibility or by adding WindowStateListener

some important notice here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Try

if(Frame.getFrames().length == 0) {
    // work here.
}

(Frame is java.awt.Frame, that is the parent of JFrame, so you will capture them, too).

marc
  • 6,103
  • 1
  • 28
  • 33
2

Thanks to everyone for answers. With this help I managed to get a working solution:

Strange, it doesn't works with windowClosed. Only works with windowClosing.

Code

public final class CloseOnLastWindowListener extends WindowAdapter {

    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println("\tCLOSING!!!");

        int nRelevant = 0;
        for (Window w : Window.getWindows()) {
            // get only visible windows, except the one being closed
            if (w != e.getWindow() && w.isVisible()) {
                System.out.println("\tRELEVANT: " + w);
                ++nRelevant;
            } else {
                System.out.println("\tirrelevant: " + w);
            }
        }

        if (nRelevant == 0) {
            System.out.println("\tEXIT!!!");
            System.exit(0);
        }
    }
}
Community
  • 1
  • 1
1

I need to know on a window close event, if any other window is still visible. If not, System.exit(0) would be called.

Just use:

frame.dispose();

When the last window is close the JVM will exit automatically.

Or when you create you frames and dialogs use:

frame.setDefaultCloseOperation(JFrema.DISPOSE_ON_CLOSE);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • "When the last window is close the JVM will exit automatically": that's true only if not used with Java Web Start, and provided you have no non-daemon running thread. You also may have problems when using modal dialogs for which Swing opens a parent frame that you need to dispose manually. – jfpoilpret Aug 16 '11 at 09:38
  • "frame.setDefaultCloseOperation(JFrema.DISPOSE_ON_CLOSE);": this doesn't answer OP's question, he wants that the LAST closed frame exits the application, not just any of them. – jfpoilpret Aug 16 '11 at 09:39
  • @jfpoilpret -- Good objection, by accident, I was going to use Webstart. – java.is.for.desktop.indeed Aug 16 '11 at 13:24
  • @jfpoilpret, Hmm, I just updated my [Alpha Container](http://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/) webstart app to use "DISPOSE_ON_CLOSE". When I click the "Launch" button the app starts and it shows up on the Windows Task Manager. When I click on the close button it closes and is removed from the Windows Task Manager. Does this not mean the JVM has exited? – camickr Aug 16 '11 at 17:51
  • @camickr yeah, that's strange, maybe they've fixed that bug in recent Java Plugins? Kirill had written a good post about it a few years ago: http://www.pushing-pixels.org/2008/01/14/when-dispose_on_close-met-webstart.html – jfpoilpret Aug 23 '11 at 07:51
  • Unfortunately I couldn't find the original bug ID, to check its status. If someone has got the reference, please post it in a comment! – jfpoilpret Aug 23 '11 at 08:00