How to Close JFrame on Button Click in Java

Hello, enthusiasts. In this tutorial, we will learn and focus on how to close JFrame on a button click.

Closing JFrame on Button Click in Java

In this tutorial, I will first create a button and then add it to the frame. Then, use an action listener event that will close the frame with the click of the button. I have already discussed how to exit JFrame on close in my earlier post, so please do check it out. I will also be using a different cursor when clicking the button in the JFrame.

Please do read the below posts to get more knowledge on these topics as you will learn more about Java Swing, Java AWT, their uses, JFrame constants to close the frame, creating custom cursors, etc.

Adding a Button to JFrame

Firstly, we create a JFrame in Java using the Java Swing library. We specify the location of the frame and set the visible component to true to display the frame on our screen. Secondly, to create a JButton using the Swing library we first extend our main class to the JFrame class.

Now, we use the JButton class to create a button in Java and name it as “Close JFrame!”. We will now add a hand cursor when we hover over the button. Our main aim now is to close the JFrame by clicking the button. Thus, we will use the ActionListener class to handle the click events of the button.

Let’s see the code snippet:

//Creating a JButton and adding it to the frame

//Creating a button with a text
JButton button=new JButton("Close JFrame!"); 
//Setting the cursor to the hand symbol
button.setCursor(new Cursor(Cursor.HAND_CURSOR));  

Output:

How to Close JFrame on Button Click in Java

Setting the ActionListener Class

The ActionListener class gets notified on click events and is responsible for all the events of the button. It is a part of the Java AWT package. We now initialize the ActionListener class and create a function inside the class for handling all the click events.

We use the frame.dispose() method, which places a request to the garbage collector to free the memory. As a result, it then deallocates the memory and causes the JFrame to close. However, another alternative is to use the System.exit(0) method which terminates the program and hereby closes the JFrame.

Let us see the code:

//Required Imports 
import javax.swing.*;
import java.awt.*;

public class CloseJframeByButton extends JFrame {
    public static void main(String[] args) {

        //Creating a new Java JFrame with a title
        JFrame frame = new JFrame("Close JFrame on Button Click"); 
        frame.setLayout(null); 
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setBounds(100, 200, 350, 200); 

        //Creating a JButton and adding it to the frame

        //Creating a button with a text
        JButton button=new JButton("Close JFrame!"); 
        //Setting the cursor to the hand symbol
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));  
        
        //Setting the action listener for the button to close the frame on click
        button.addActionListener(event->{
            frame.dispose();
        });
        
        //Adding the objects in the container frame and setting the content pane layer
        frame.setContentPane(button);
        frame.setVisible(true); //Displaying the Java frame
        
     
    }
}

Output:

Thanks for reading!

Leave a Reply

Your email address will not be published.