Please enable JavaScript to use CodeHS

Using Java Swing Buttons - Events

This tutorial builds on the Java Swing Buttons - Layout tutorial. In this tutorial, students will learn how to read the status of check boxes, radio buttons, and create button events.

By David Burnham

In this tutorial, you are going to look at adding events to buttons and using these to read the state of checkboxes and radio buttons. This tutorial is going to start with the basic layout that was established in the Java Swing Buttons - Layout tutorial.


Refactoring the Layout


In the original layout, the program used one static method to create and display the layout. While this worked well for creating a layout and can work for adding basic events, this tutorial is going to use a slightly different approach that will allow you to use a separate method for handling more complex event procedures. To do this, the program will be refactored to create an instance of the class and to use instance variables.


Notice in the refactored code below how the createAndShowGUI() is no longer static and the main() method creates an instance of your class. The buttons, checkboxes, and radio buttons are also brought out to be instance variables. Otherwise, the layout is the same.

Adding Button Events


The first thing that you will tackle is adding button events using the ActionListener. The action listener is an interface and when you use the interface, you need to add the actionPerformed method to handle the event. The actionPerformed method will automatically be called when one of your buttons is clicked.

This tutorial is not going to get into the details of how an interface works, but you can use the interface by adding implements ActionListener to the class header:

public class ComponentDemo implements ActionListener 

For your program to implement the action listener interface, you need to add the following method into your program:

public void actionPerformed(ActionEvent e)

This method will be the method that is called when a button is clicked, however, to associate the method with your button, you need to add the action listeners for each button:

start.addActionListener(this);
stop.addActionListener(this);

Notice that each button has the listener attached using the addActionListener method. This method takes an instance of an object, in this case you reference the current object that you created using the this keyword.


Handling the Event


Now that you have associated a button click with the actionPerformed method, you can write a basic method to handle that click and print out which button was pressed.

To do this, you use the ActionEvent variable e and check which button was clicked with a basic if statement.

Reading the Values of Checkboxes and Radio Buttons


Now that you have a button and an event, you can use these to check the values of things like checkboxes and radio buttons. Both of these buttons use the same method to check their status, isSelected(). This method returns true when the box or button is checked, otherwise, it returns false.

Here is an example of how it can be used with the cb1 variable, which is a checkbox. This code segment would be placed inside the actionPerformed method.

if (cb1.isSelected()) {
  System.out.println("Checkbox 1 IS selected");
}
else {
  System.out.println("Checkbox 1 IS NOT selected");
}


Check out the example below, and then you can work on refining the code slightly.


Refactoring the Code


Now that you can see how to check the status of your checkboxes and radio buttons, the status can easily be a little overwhelming, especially if you have a lot of boxes and buttons to check.

In this final example, check out how the code has been refactored. Here are some of the changes to help create a more complete and efficient code.

  • Created two ArrayLists, one to hold all checkboxes and one to hold all radio buttons.
  • Adding the ArrayLists as instance variables means that the individual checkboxes and radio buttons do not have to be instance variables. They can be variables that get inserted into the ArrayList instance variables.
  • Loop through the two ArrayLists each time a button is clicked. In doing so, you can quickly check the status for all your boxes and buttons.

Notice that the print statement has been made more generic and utilizes the .getText() method to return the label value for the selected box/button option.