How to use Exit button in Swing Application?

jframe close button event
how to close current jframe in java swing
java swing button close
close a swing window
how to close jpanel on button click
close a frame with a button
java button close form
how to perform action on button in java swing

I am new in Java Form Application development using Swing. What is the suitable code for exit button that will exit the application?

For example: In C# (.NET Framework) we use Application.Exit(); as the code for exit what button. What is the equivalent code in Java to exit an application?


Now for a bit of explanation. The closest equivalent to as seen in the question is..


But that kills the entire JRE irrespective of other non-daemon threads that are running. If they are running, they should be shut down or cleaned up explicitly.

If there are no other non-daemon threads running, DISPOSE_ON_CLOSE will dispose the current JFrame and end the virtual machine.

create an exit button in java?, Java Swing Exit Button Code. In this example, we will have a button. When we click on the button, the application terminates. package  3. Close resources before exiting JFrame and TCP communication in Java 1. I'm writing a chat based application on TCP communication. I'm using NetBeans and I want to add functionality to the default EXIT_ON_CLOSE when exiting JFrame.

@AndrewThompson is right about JFrame.EXIT_ON_CLOSE but you can take steps to avoid just killing stuff.

JFrame.EXIT_ON_CLOSE is just fine, if you handle cleanup in an overridden JFrame.processWindowEvent(WindowEvent) checking for WindowEvent.WINDOW_CLOSING events.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class ExitJFrame extends JFrame {

    public ExitJFrame() {
        setLayout(new BorderLayout());


        JButton button = new JButton("Exit");

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                        new WindowEvent(
                                ExitJFrame.this, WindowEvent.WINDOW_CLOSING));

        setSize(200, 200);

    protected void processWindowEvent(WindowEvent e) {
        // more powerful as a WindowListener, since you can consume the event 
        // if you so please - asking the user if she really wants to exit for
        // example, do not delegate to super if consuming.
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
        } else {        

    private void doCleanup() {
        final JDialog dialog = new JDialog(this, true);
        dialog.setLayout(new BorderLayout());
        JProgressBar progress = new JProgressBar();
        dialog.add(new JLabel("Waiting for non-daemon threads to exit gracefully..."), BorderLayout.NORTH);

        Timer timer = new Timer(2000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {

    public static void main (String[] args) {
        new ExitJFrame().setVisible(true);


How to exit Application When clicked on JButton Java Swing/SWT , Set frame.dispose() on the click of a button to close JFrame. At first create a button and frame −JFrame frame = new JFrame(); JButton button  This example explains you how a frame can be closed on an action event i.e. a frame should be close when clicked on the button on a frame. In this example we will use the dispose() method to close the frame. JFrame Close On Button Click. In this section we will discuss about how to close frame on click on the button.

If you want to create a button which exits the application when the user clicks it, try this:

JButton exit = new JButton("exit");

ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent e) {


Now when you press on that button the application will exit.

Java Swing Exit Button, JFrame close example: How to close your JFrame when the user presses in a Java client-side application, and the user presses the exit button on the methods you can use a WindowAdapter instead of implementing the  The class JButton is an implementation of a push button. This component has a label and generates an event when pressed. It can also have an Image. Creates a button with no set text or icon. Creates a button where properties are taken from the Action supplied. Creates a button with an icon. Creates a button with the text.

Very simple answer is as @Andrew Thompson said.




and if u want to make it in other way by code. then u can put this code anywhere. in events.


hope this will help.

How to close JFrame on the click of a Button in Java, In this section we will discuss about how to close frame on click on the button. In this example we will use the dispose() method to close the frame. which will demonstrate you about how to create a frame in Java and close this frame by clicking on a button . The GUI will contain a JFrame and a JButton on frame. First declare the buttons and initialize them: JButton exitButton = new JButton("Exit"); JButton clearButton = new JButton("Clear"); Then, make sure the butPanel has a layout and the panel is added to the content pane:

If you're talking about a JButton that you want to put into your form, then you want to invoke JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) in your constructor for the form, and then your button handler can invoke


to close the form.

JFrame close example: How to close your JFrame when the user , If you want to close or terminate your java application before this, your only option is to use System.exit(int status) or Runtime. which terminates Java swing GUI program when you click close button on JFrame window. 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. copied from Peter Lang

JFrame Close On Button Click, I'm writing a chat based application on TCP communication. I'm using NetBeans and I want to add functionality to the default EXIT_ON_CLOSE when exiting  Pressing the close (X) button on your window causes the operating system to generate a close window event and send it to your Java application. The close window event is processed by the AWT event loop in your Java application which will exit the application in response to the event. If you do not call this method the AWT event loop may not

How to Close an Application Properly, In a complex application with many frames, you probably want only to close the The setDefaultCloseOperation() method is used to specify one of several options for the close button. Use one of the following constants to specify your choice:. JButton class is used for adding platform independent buttons to a swing application. In this tutorial we will learn how to create a button in Swing application and how to tweak their appearance as per the requirement. I have also shared some code snippets that may be useful for you while developing a Swing application.

Exit Button « JFrame « Java Swing Q&A, Click the Launch button to run the Frame Demo using Java™ Web Start ( download JDK 7 or later ). Alternatively, to compile and run the example yourself, consult the example index. Bring up two windows, both with look-and-feel-provided decorations, but with different icons. The Java look and feel displays the icons in its window decorations