PDF download Download Article PDF download Download Article

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.

Method 1
Method 1 of 2:

Using javax.swing.JFrame

PDF download Download Article
  1. 1
    Obtain an instance of a JFrame, or create a new one.
  2. 2
    Set default close operation. Default close operation is set using the setter method inside the JFrame class setDefaultCloseOperation that 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 between HIDE_ON_CLOSE and DISPOSE_ON_CLOSE is 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 a WindowListener to the frame and overriding windowClosing method. 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);
        		}
        	}
        });
        
  3. Advertisement
Method 2
Method 2 of 2:

Using java.awt.Frame

PDF download Download Article
  1. 1
    Obtain an instance of a Frame, or create a new one.
  2. 2
    Add window listener. Call addWindowListener method on the instance. The required argument is WindowListener. You can either implement every method of the WindowListener interface or override only the methods you need from WindowAdapter class.
  3. 3
    Handle window closing event. Implement windowClosing method from WindowListener interface or override it from WindowAdapter class. There are two ways of closing a window:
    • Dispose the window after the close button is clicked:
      • Call dispose method inside windowClosing method.
      • frame.addWindowListener(new WindowAdapter() {
        	@Override
        	public void windowClosing(WindowEvent e) {
                        // Dispose the window after the close button is clicked.
        		dispose();
        	}
        });
        
    • Terminate the program after the close button is clicked:
      • Call System.exit method inside windowClosing method.
      • frame.addWindowListener(new WindowAdapter() {
        	@Override
        	public void windowClosing(WindowEvent e) {
                        // Terminate the program after the close button is clicked.
        		System.exit(0);
        	}
        });
        
  4. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

  • Swing is preferred over AWT since the latter is really outdated.
  • Using WindowAdapter you don't have to implement each and every method WindowListener contract tells us to, but only the ones we need.
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Check Null in JavaCheck Null in Java
Compile & Run Java Program Using Command PromptA Beginner’s Guide to Compiling & Running Java Programs
Check Your Java Version in the Windows Command LineUse Easy Windows CMD Commands to Check Your Java Version
Do Division in Java Do Division in Java (Integer and Floating Point)
Set Java Home Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Compile and Run Java Program by Notepad Compile and Run Java Programs Using Notepad++
Check Java Version on a Mac Quickly and Easily Check Java Version on a Mac
Create an Executable File from EclipseCreate an Executable File from Eclipse
Add JARs to Project Build Paths in Eclipse (Java)Add JARs to Project Build Paths in Eclipse (Java)
Update JavaUpdate Java
Calculate Percentage in JavaCalculate Percentage in Java
Program in JavaProgram in Java
Call a Method in JavaCall a Method in Java
Create JAR File Create a JAR File With Eclipse
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 10 people, some anonymous, worked to edit and improve it over time. This article has been viewed 152,518 times.
How helpful is this?
Co-authors: 10
Updated: June 3, 2021
Views: 152,518
Categories: Featured Articles | Java
Thanks to all authors for creating a page that has been read 152,518 times.

Is this article up to date?

Advertisement