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.
Here would be your options:
Not only to close the JFrame but also to trigger WindowListener events, try this:
Best way to close a Swing frame programmatically is to make it behave like it would when the "X" button is pressed. To do that you will need to implement WindowAdapter that suits your needs and set frame's default close operation to do nothing (DO_NOTHING_ON_CLOSE).
Initialize your frame like this:
You can close the frame programmatically by sending it the WINDOW_CLOSING event, like this:
This will close the frame like the "X" button was pressed.
Exiting from Java running process is very easy, basically you need to do just two simple things:
System.exit(...)
at at application's quit point. For example, if your application is frame based, you can add listenerWindowAdapter
and and callSystem.exit(...)
inside its methodwindowClosing(WindowEvent e)
.Note: you must call
System.exit(...)
otherwise your program is error involved.System.exit(...)
at right point, but It does not mean that the method can be called always, because unexpected java exceptions may prevent the method from been called.This is strongly related to your programming skills.
** Following is a simplest sample (
JFrame
based) which shows you how to call exit methodIf you do not want your application to terminate when a JFrame is closed, use: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
instead of: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
From the documentation:
DO_NOTHING_ON_CLOSE (defined in WindowConstants)
: Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.HIDE_ON_CLOSE (defined in WindowConstants)
: Automatically hide the frame after invoking any registered WindowListener objects.DISPOSE_ON_CLOSE (defined in WindowConstants)
: Automatically hide and dispose the frame after invoking any registered WindowListener objects.EXIT_ON_CLOSE (defined in JFrame)
: Exit the application using the System exit method. Use this only in applications.might still be useful: You can use
setVisible(false)
on your JFrame if you want to display the same frame again. Otherwise calldispose()
to remove all of the native screen resources.copied from Peter Lang
https://stackoverflow.com/a/1944474/3782247