JCheckBox in Java Swing

JCheckBox in Java Swing is a class from Java Swing library that represents a graphical component that allows the user to select one or more options from a set of options. A JCheckBox component typically displays a box that can be checked or unchecked, along with a label that describes the option being represented.

Single JCheckBox Example

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

public class SingleJCheckBoxDemo extends JFrame {
	public SingleJCheckBoxDemo() {
		// Create a JCheckBox component
		JCheckBox checkBox = new JCheckBox("Option 1");

		// Add the JCheckBox component to the JFrame
		add(checkBox);

		// Set the JFrame properties
		setTitle("JCheckBox Demo");
		setSize(300, 200);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

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

The output of the above code is:

Single JCheckBox Output in Java Swing

Multiple JCheckBox Example

import java.awt.FlowLayout;
import java.util.Arrays;
import java.util.List;

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

public class MultipleJCheckBoxDemo extends JFrame {
	List<JCheckBox> checkBoxes;

	public MultipleJCheckBoxDemo() {
		// Create a JCheckBox component
		checkBoxes = Arrays.asList(new JCheckBox("Option 1"), new JCheckBox("Option 2"), new JCheckBox("Option 3"));

		// Add the JCheckBox component to the JFrame
		for (JCheckBox checkBox : checkBoxes) {
			add(checkBox);
		}

		setLayout(new FlowLayout(FlowLayout.CENTER));

		// Set the JFrame properties
		setTitle("JCheckBox Demo");
		setSize(300, 200);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

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

The output of the above code is:

Multiple JCheckBox Output in Java Swing

Add event in JCheckBox

To add an event in JCheckBox, we need to add an action listener for each JCheckBox so that when the user clicks on the check box it displays the selected check box value as output. See the example below:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MultipleJCheckBoxDemo extends JFrame {
	List<JCheckBox> checkBoxes;

	public MultipleJCheckBoxDemo() {
		// Create a JCheckBox component
		checkBoxes = Arrays.asList(new JCheckBox("Option 1"), new JCheckBox("Option 2"), new JCheckBox("Option 3"));

		// Add the JCheckBox component to the JFrame
		for (JCheckBox checkBox : checkBoxes) {
			add(checkBox);
		}

		setLayout(new FlowLayout(FlowLayout.CENTER));

		// Set the JFrame properties
		setTitle("JCheckBox Demo");
		setSize(300, 200);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);

		// Adding an event to checkbox to display the vlaue when user selects a
		// checkbox.
		checkBoxes.forEach(checkBox -> {
			checkBox.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if (checkBox.isSelected())
						JOptionPane.showMessageDialog(null, checkBox.getText());
				}
			});
		});
	}

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

Output:

Multiple JCheckBox Output in along with event in Java Swing
Sharing Is Caring: