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.
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.
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.
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:
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 usingSystem.exit().
Running code when the window closes
To do extra work while the frame is closing, add a window listener and override windowClosing:
Options compared
| Method | Description | Use Case |
setVisible(false) | Hides the JFrame window | Temporarily hide window without terminating application |
dispose() | Releases resources and disposes JFrame | Permanently close a window when it's no longer needed |
System.exit(0) | Terminates JVM and closes application | End the entire application |
setDefaultCloseOperation() | Determines operation on close click | Define behavior for JFrame close button |
Related reading
- How to properly handle expected errors in Hystrix fallback?
- How to properly import a selfsigned certificate into Java keystore that is available to all Java applications by default?
- How to properly match varargs in Mockito
- how to properly specify database schema in spring boot?
- How to properly stop the Thread in Java?
- How to proxy a HTTP video stream to any amount of clients through a Spring Webserver
- How to quickly and conveniently create a one element arraylist
- How to randomize two ArrayLists in the same fashion?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.