What's the correct way to get a JFrame
to close, the same as if the user had hit the X
close button, or pressed Alt+F4 (on Windows)?
I have my default close operation set the way I want, via:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
It does exactly what I want with the aforementioned controls. This question isn't about that.
What I really want to do is cause the GUI to behave in the same way as a press of X
close button would cause it to behave.
Suppose I were to extend WindowAdaptor
and then add an instance of my adaptor as a listener via addWindowListener()
. I would like to see the same sequence of calls through windowDeactivated()
, windowClosing()
, and windowClosed()
as would occur with the X
close button. Not so much tearing up the window as telling it to tear itself up, so to speak.
If by Alt-F4 or X you mean "Exit the Application Immediately Without Regard for What Other Windows or Threads are Running", then
System.exit(...)
will do exactly what you want in a very abrupt, brute-force, and possibly problematic fashion.If by Alt-F4 or X you mean hide the window, then
frame.setVisible(false)
is how you "close" the window. The window will continue to consume resources/memory but can be made visible again very quickly.If by Alt-F4 or X you mean hide the window and dispose of any resources it is consuming, then
frame.dispose()
is how you "close" the window. If the frame was the last visible window and there are no other non-daemon threads running, the program will exit. If you show the window again, it will have to reinitialize all of the native resources again (graphics buffer, window handles, etc).dispose()
might be closest to the behavior that you really want. If your app has multiple windows open, do you want Alt-F4 or X to quit the app or just close the active window?The Java Swing Tutorial on Window Listeners may help clarify things for you.
You have to insert the call into the AWT message queue so all the timing happens correctly, otherwise it will not dispatch the correct event sequence, especially in a multi-threaded program. When this is done you may handle the resulting event sequence exactly as you would if the user has clicked on the [x] button for an OS suppled decorated JFrame.
If you have done this to make sure the user can't close the window:
Then you should change your
pullThePlug()
method to beI found this to be the only way that plays nice with the
WindowListener
andJFrame.DO_NOTHING_ON_CLOSE
.If you want the GUI to behave as if you clicked the
X
close button then you need to dispatch a window closing event to theWindow
. TheExitAction
from Closing An Application allows you to add this functionality to a menu item or any component that usesAction
s easily.I have tried this, write your own code for formWindowClosing() event.
This asks user whether he want to exit the Frame or Application.
If you really do not want your application to terminate when a JFrame is closed then,
use :
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
instead of :
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Here's a synopsis of what the solution looks like,