JToggleButton, JCheckBox and JRadioButton of Swing

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

In the previous article, we dealt with the JButton class. In addition to the JButton class, Swing has other classes that handle buttons. In this article, we will introduce the basic usage of the following three classes.

  • JToggleButton
  • JCheckBox
  • JRadioButton

We will use the following program as a template.

Source
import javax.swing.JFrame;

public class Test extends JFrame {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(350, 250);

        init();

        setVisible(true);

    }

    private void init() {
        // installation process
    }

}

JToggleButton

JToggleButton is similar to JButton, but it can be used to transition between the following two states when the button is pressed.

  • Pressed down
  • Not pressed down

In the case of JButton, when we press down on the button with the mouse, it will be in the pressed state, and when we release the mouse button, it will automatically be in the unpressed state. On the other hand, JToggleButton can be used like an on/off switch.

JToggleButton#isSelected method returns the state that the button is pressed. In the following program, JToggleButton and JButton are added to a JFrame. When a JButton is pressed, the console will show whether the JToggleButton is pressed or not.

Source
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class Test2 extends JFrame {

    public static void main(String[] args) {
        new Test2();
    }

    public Test2() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(350, 250);

        init();

        setVisible(true);

    }

    private void init() {
        setLayout(new FlowLayout());

        JToggleButton toggleButton = new JToggleButton("Test ToggleButton");
        add(toggleButton);

        JButton button = new JButton("JButton");
        add(button);
        button.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                System.out.println(toggleButton.isSelected());
            }
        });
    }
}
Screen when the result of the isSelected() method is false
001_JToggleButtonFalse.png
Screen when the result of the isSelected() method is true
001_JToggleButtonTrue.png

If the button is pressed, the isSelected method returns true. By the way, this method is inherited from AbstractButton.

JCheckBox

JCheckBox is a class that inherits from JToggleButton. It displays a rectangle for checking.

The following is an example of use.
Two JCheckBox objects are created and added to the JFrame. We have also added a JButton object. When the button is pressed, the console will display the pressing status of each JCheckBox.

Source
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class Test3 extends JFrame {

    public static void main(String[] args) {
        new Test3();
    }

    public Test3() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(350, 250);

        init();

        setVisible(true);

    }

    private void init() {
        setLayout(new FlowLayout());

        JCheckBox checkBox1 = new JCheckBox("JCheckBox1");
    	add(checkBox1);

    	JCheckBox checkBox2 = new JCheckBox("JCheckBox2");
    	add(checkBox2);

    	JButton button = new JButton("JButton");
    	add(button);
    	button.addMouseListener(new MouseAdapter() {
    	    public void mouseClicked(MouseEvent e) {
    	        System.out.println("checkBox1 is Selected: " + checkBox1.isSelected());
    	        System.out.println("checkBox2 is Selected: " + checkBox2.isSelected());
    	    }
    	});
    }

}

Initially, the checkbox is not checked. The following is the execution result when the JButton is pressed with checkbox2 checked.

002_JCheckBox.png
Execution results
checkBox1 is Selected: false
checkBox2 is Selected: true

You can see that the isSelected() method returns true/false corresponding to the state of the checkbox.

JRadioButton

JRadioButton inherits from JToggleButton. This object will display a circle for button presses. To treat multiple buttons as an exclusive group of radio buttons, use the ButtonGroup class.

To get the selected button, use the ButtonModel#getSelection method. However, the return value of this method is the ButtonModel interface. You can use ButtonModel#getActionCommand method to get the action command string of the button.

The following example creates two JRadioButton objects and sets a specific action command for each.
We also add each of them to one group. It also creates a JButton object. When that button is pressed, the action command set for the selected JRadioButton will be output to the console. Initially, the JRadioButton is not selected, so we write a process to check for null.

Source
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class Test4 extends JFrame {

    public static void main(String[] args) {
        new Test4();
    }

    public Test4() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(350, 250);

        init();

        setVisible(true);

    }

    private void init() {
        setLayout(new FlowLayout());

        JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
        radioButton1.setActionCommand("R1");
        add(radioButton1);

        JRadioButton radioButton2 = new JRadioButton("JRadioButton1");
        radioButton2.setActionCommand("R2");
        add(radioButton2);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);

        JButton button = new JButton("JButton");
        add(button);
        button.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                ButtonModel buttonModel = buttonGroup.getSelection();
                System.out.println(buttonModel == null ? "not selected" : buttonModel.getActionCommand());
            }
        });
    }
}
Screen when “not selected" is output
003_JRadioButton_1.png
Screen when “R1" is output
003_JRadioButton_2.png
Screen when “R2" is output
003_JRadioButton_3.png

Note that if you add JRadioButtons to a group, they become exclusive buttons. Once a button belonging to a group is selected, it is not possible to return to the state where all buttons belonging to that group are unselected. If you don’t add a JRadioButton to a group, you can use it just like a checkbox. You can make it unselected by pressing the button.

That’s all. I hope this is helpful to you.

Articles next and previous in the same category