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.
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
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).
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
.
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);
}
}
}
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);