How to Check if a Button is Clicked in a Java Swing Application

Java


In this article, we show how we can check to see if a button is clicked in a Java Swing application.

To be able to check if a button is clicked using Java, we create a button and add an event handler (or event listener) to the button, so that when the button is clicked, a method can be called. We can create the method to do anything such as output that the button was clicked or anything we want it to do.



Alright, so now, let's go through the code.

Since this is a Swing application, we have to import the javax.swing package. We also have to import the java.awt.event package because this contains the ActionListener interface.

So we create our JFrame class named Clickprogram. It extends the JFrame class because we're creating a frame.

We then create our main method. Since this is a singularly program, it needs a main method in order for the code to run.

All we do in this main method is create an instance of the Clickprogram class so that we have a frame.

We then create the constructor of the Clickprogram class. This constructor is so that we can initialize all the values of the frame that we create. Since the class we create is a frame class (it extends JFrame), we now add in what we want in the frame.

So the first thing we do in the constructor is set the size of the frame. In this case, we set the frame to a width of 300px and a height of 200px.

In almost any frame you build in Java, you want to add in the line, setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. This is because when a window is created, it doesn't terminate upon pressing the X button at the top right of the window. It simply vanishes but it doesn't truly terminate operations. So in order to completely the end the GUI frame program, you have to specify this line. This completely ends the running of the program once the user Xs out.

We then set the title of the frame to "Click Event".

What we do next is we create an instance of the Clicklistener class. This is the class that you see later in the code that implements the ActionListener interface. For now, just realize we create an instance of this class and later when we create our button, we put this instance as a parameter to the addActionListener method implemented on the button. This ties an event handler to the button so that it calls this instance of the Clicklistener class.

We then create a panel. Usually when you add components such as buttons, text fields, etc to a frame, you first add to a panel. And then you add the panel to the frame. So we create a panel named panel1.

We then create a button named button1 that contains the phrase, "Click here".

To this button, we add the addActionListener and inside of this method, we put click. This click represents an instance of the Clicklistener class. We'll get to this below.

We then add to the panel, button1.

We then add to this frame, panel1. Similarly, you could leave off this and it would still work.

We then make the frame visible.

The next block of code is now the Clicklistener class which implements the ActionListener interface.

We create a method actionPerformed, which in its parameter contains the ActionEvent class.

If the source of the action created is equal to button1, then we change the text of the button to, "The button has been clicked".

And this is all that is required to check if a button is clicked using Java.


Related Resources

Java ArrayList Tutorial




HTML Comment Box is loading comments...