CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JCheckBox in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

JCheckBox in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

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.

Table of Contents

Single JCheckBox Example
Multiple JCheckBox Example
Add event in JCheckBox

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

Related Posts:

  • User Defined Exception in Java
  • Event Handling in Java Swing
  • Exception Handling in Java
  • MySQL Commands for Developers
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Control Statements in Java
Tags:javaswing
Was this article helpful?
← Previous ArticleObject Class in Java – The Root of All Classes
Next Article →JFrame in Java Swing

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap