Java
Programming
JFrame
GUI Development
Code Tutorial

How to programmatically close a JFrame in Java Swing

Codemia Knowledge Hub ·

Call frame.dispose() to close a JFrame and release its native resources. Use setVisible(false) if you only want to hide it, or System.exit(0) if closing the window should end the whole application. setDefaultCloseOperation() controls what the title bar's close button does.

A JFrame, from the javax.swing package, is a top-level window with a title and border that serves as the main window of a Swing application.

Hide it with setVisible(false)

This hides the window from the user. It does not close the application or release resources; the frame just stops being visible.

java
1JFrame frame = new JFrame("My Application");
2frame.setSize(300, 200);
3frame.setVisible(true);
4// Hide the JFrame
5frame.setVisible(false);

Close it with dispose()

dispose() releases the native screen resources the frame uses. Use it when the frame is no longer needed and you want those resources returned to the system.

java
1JFrame frame = new JFrame("My Application");
2frame.setSize(300, 200);
3frame.setVisible(true);
4// Dispose the JFrame
5frame.dispose();

End the application with System.exit(int status)

This terminates the running Java Virtual Machine (JVM). The argument is a status code, and by convention a nonzero code means abnormal termination. It ends the entire application, not just the frame.

java
1JFrame frame = new JFrame("My Application");
2frame.setSize(300, 200);
3frame.setVisible(true);
4// Terminate JVM and close the application
5System.exit(0);

Setting what the close button does

setDefaultCloseOperation() defines what happens when the user closes the frame, for example by clicking the close button on the title bar:

java
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

The common arguments are:

  • JFrame.DO_NOTHING_ON_CLOSE: ignore the close operation.
  • JFrame.HIDE_ON_CLOSE: hide the JFrame but keep the application running.
  • JFrame.DISPOSE_ON_CLOSE: dispose of the JFrame and release resources.
  • JFrame.EXIT_ON_CLOSE: exit the application using System.exit().

Running code when the window closes

To do extra work while the frame is closing, add a window listener and override windowClosing:

java
1frame.addWindowListener(new WindowAdapter() {
2    public void windowClosing(WindowEvent we) {
3        System.out.println("Window is closing!");
4        frame.dispose();  // Or any other operation
5    }
6});

Options compared

MethodDescriptionUse Case
setVisible(false)Hides the JFrame windowTemporarily hide window without terminating application
dispose()Releases resources and disposes JFramePermanently close a window when it's no longer needed
System.exit(0)Terminates JVM and closes applicationEnd the entire application
setDefaultCloseOperation()Determines operation on close clickDefine behavior for JFrame close button

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms