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 W
If 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 call dispose()
to remove all of the native screen resources.
https://stackoverflow.com/a/1944474/3782247