How to Close a Window in Java
This article will show you how to close a window in Java. Closing a window is much easier using Swing's JFrame, but it's also doable using AWT's Frame. Obtain an instance of a JFrame, or create a new one. Set default close operation....
Method 1 of 2:
Using javax.swing.JFrame
-
Obtain an instance of aJFrame, or create a new one. -
Set default close operation. Default close operation is set using the setter method inside theJFrameclasssetDefaultCloseOperationthat determines what happens when the close button is clicked and takes the following parameters:WindowConstants.EXIT_ON_CLOSE- Closes the frame and terminates the execution of the program.WindowConstants.DISPOSE_ON_CLOSE- Closes the frame and does not necessarily terminate the execution of the program.WindowConstants.HIDE_ON_CLOSE- Makes the frame appear like it closed by setting its visibility property to false. The difference betweenHIDE_ON_CLOSEandDISPOSE_ON_CLOSEis that the latter releases all of the resources used by the frame and its components.WindowConstants.DO_NOTHING_ON_CLOSE- Does nothing when the close button is pressed. Useful if you wish to, for example, display a confirmation dialog before the window is closed. You can do that by adding aWindowListenerto the frame and overridingwindowClosingmethod. Example of the custom close operation:frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Ask for confirmation before terminating the program. int option = JOptionPane.showConfirmDialog( frame, "Are you sure you want to close the application?", "Close Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (option == JOptionPane.YES_OPTION) { System.exit(0); } } });
Method 2 of 2:
Using java.awt.Frame
-
Obtain an instance of aFrame, or create a new one. -
Add window listener. CalladdWindowListenermethod on the instance. The required argument isWindowListener. You can either implement every method of theWindowListenerinterface or override only the methods you need fromWindowAdapterclass. -
Handle window closing event. ImplementwindowClosingmethod fromWindowListenerinterface or override it fromWindowAdapterclass. There are two ways of closing a window:- Dispose the window after the close button is clicked:
- Call
disposemethod insidewindowClosingmethod.
frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Dispose the window after the close button is clicked. dispose(); } });
- Call
- Terminate the program after the close button is clicked:
- Call
System.exitmethod insidewindowClosingmethod.
frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Terminate the program after the close button is clicked. System.exit(0); } });
- Call
- Dispose the window after the close button is clicked:
Discover more
frame close methodShare by
Lesley Montoya
Update 05 March 2020
You should read it
- How to close the application on Android
- Method in HTTP
- How to show the alignment frame in Word
- How to Close Apps in Windows 8
- How to frame text in Word
- The Quiet Details That Make a Sports Betting Platform Feel Reliable
- Instructions on creating toy set images with ChatGPT AI
- How are AI agents changing the journalism industry?
- How to Compare Two Dates in Java
- How to Update Java
- How to Determine Java Version




