Select One Item at a Time from JCheckBox in Java



In this article, we will learn to select one item at a time from a JCheckBox in Java. When creating Java Swing applications, you might have situations where you need checkboxes to behave like radio buttons, such that a box can be checked singly at any given time.

JCheckBox

A JCheckBox can extend JToggleButton, and it can be a small box that is either checked or unchecked. When we click on a JCheckBox, it changes from checked to unchecked or vice versa automatically.

Syntax

The following is the syntax for JCheckBox initialization:

JCheckBox checkBox = new JCheckBox("Option");

A JCheckBox can generate an ActionListener or an ItemListener whenever the checkbox is changed. An isSelected() method is used to test if a checkbox is checked or not.

Selecting One Item at a Time in a JCheckBox

By default, we can select all the checkbox items at a time, but if we want to select only one item at a time, we will use the ButtonGroup class. The following is the step-by-step process for selecting one item at a time from a JCheckBox in Java:

Class Declaration and Imports

The javax.swing.* provides Swing components (JCheckBox, JFrame, etc.) while the java.awt.* provides AWT classes (Color, layout managers), and the class extends JFrame to create a window.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCheckBoxGroupTest extends JFrame {

Instance Variable & Constructor Setup

The checkBoxGroup variable is declared using ButtonGroup. Three JCheckBox components: jcb1, jcb2, and jcb3, are initialized. A JPanel creates a container named panel to organize the checkboxes.

The constructor initializes the JFrame window with the title "JCheckBoxGroup Test" and GridLayout as the layout manager.

private ButtonGroup checkBoxGroup;
private JCheckBox jcb1, jcb2, jcb3;
private JPanel panel;
public JCheckBoxGroupTest() {
   super("JCheckBoxGroup Test");
   panel = new JPanel(new GridLayout(3,0));

Checkbox Creation and Initialization

Three checkboxes are created with labels "India", "England", and "Australia". The "India" checkbox is set as checked (true) by default, while the remaining ones are unchecked (false).

jcb1 = new JCheckBox("India", true);
jcb2 = new JCheckBox("England", false);
jcb3 = new JCheckBox("Australia", false);

ButtonGroup Configuration

Creates a new ButtonGroup and adds checkboxes to the group, making them behave like radio buttons.

checkBoxGroup = new ButtonGroup();
checkBoxGroup.add(jcb1);
checkBoxGroup.add(jcb2);
checkBoxGroup.add(jcb3);

Main Method

The main method launches the application by creating an object of JCheckBoxGroupTest.

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

Example

Below is an example of selecting one item at a time from a JCheckBox in Java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCheckBoxGroupTest extends JFrame {
   private ButtonGroup checkBoxGroup;
   private JCheckBox jcb1, jcb2, jcb3;
   private JPanel panel;
   public JCheckBoxGroupTest() {
      super("JCheckBoxGroup Test");
      panel = new JPanel(new GridLayout(3,0));
      jcb1 = new JCheckBox("India", true);
      jcb2 = new JCheckBox("England", false);
      jcb3 = new JCheckBox("Australia", false);
      checkBoxGroup = new ButtonGroup();
      //add CheckBoxes to ButtonGroup
      checkBoxGroup.add(jcb1);
      checkBoxGroup.add(jcb2);
      checkBoxGroup.add(jcb3);
      panel.add(jcb1);
      panel.add(jcb2);
      panel.add(jcb3);
      add(panel);
      setSize(375, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String args[]) {
      new JCheckBoxGroupTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-08T18:46:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements