How to make a JButton unclickable after clicking on it in Java

In this tutorial, we will learn how to make a JButton unclickable after clicking on it in Java. There are many ways in which a java button can be made unclickable. Here we will learn it by the use of the setEnable() method.

So we all know how to create a button in Java. And we also know that using these buttons in Java we can perform various tasks by adding action events associated with the buttons. Likewise, here we will also add an action event button and this time the event will be to make the button unclickable after clicking it once.

At first, we will include the packages required in the program.

Then we will include a frame in which the panel is added. On this panel, we will add a button on which action event is to be carried out. We will use addActionListener(), ActionEvent() and setEnabled() on this button.

Here we will call setEnabled(false) on the button.


Then we will leave the ActionListener in place but logically allow the button not to do any actions if desired, perhaps controlled by a boolean.

There are also many secondary things like setting the bounds to the button as well as the frame, adding color to the button, etc.

Complete Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class unclickable {

    private static JFrame frame;
    private static JPanel panel;
    private static JButton button;

    public static void main(String args[]) {
        frame = new JFrame("Unclickable");
        frame.setSize(500, 500);
        // frame.setLocation(200, 200);

        panel = new JPanel();


    public static void placeC(JPanel panel) {

        button = new JButton("Click ME");
        button.setBounds(230, 230, 100, 20);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {



The window appears like this after executing the above code:

How to make a JButton unclickable after clicking on it in Java

After clicking on the button, it looks like this and becomes unclickable.

How to make a JButton unclickable after clicking on it in Java

On clicking it once, it will become unclickable.

Hope this tutorial was useful.

Also read: How to add audio on JSwing in Java

Leave a Reply

Your email address will not be published. Required fields are marked *