How do I customize JCheckBox icons?

JCheckBox can have a different icon for its states, there are default icon, a selected icon, disabled icon, pressed icon or rollover icon, etc. See the code below and have a try on it.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class JCheckBoxCustomIcon extends JFrame {
    public JCheckBoxCustomIcon() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JCheckBox Icon Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Creating checkbox with text label
        JCheckBox checkBox = new JCheckBox("Check me!");
        checkBox.setSelected(true);

        // Set default icon for checkbox
        checkBox.setIcon(new ImageIcon("icon.png"));
        // Set selected icon when checkbox state is selected
        checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
        // Set disabled icon for checkbox
        checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
        // Set disabled-selected icon for checkbox
        checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
        // Set checkbox icon when checkbox is pressed
        checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
        // Set icon when a mouse is over the checkbox
        checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
        // Set icon when a mouse is over a selected checkbox
        checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

        getContentPane().add(checkBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JCheckBoxCustomIcon().setVisible(true));
    }
}

How do I get or set the state of JCheckBox?

This simple example shows you how to get or set the state of a JCheckBox. The method to set the state is JCheckBox.setSelected(boolean) and the method for getting the state is JCheckBox.isSelected() which return a boolean value.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class CheckBoxState extends JFrame {
    public CheckBoxState() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JCheckBox Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Creating checkbox with text label
        JCheckBox checkBox = new JCheckBox("Check me!");
        checkBox.setSelected(true);

        // Get checkbox selection state
        boolean selected = checkBox.isSelected();
        if (selected) {
            System.out.println("Check box state is selected.");
        } else {
            System.out.println("Check box state is not selected.");
        }

        getContentPane().add(checkBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new CheckBoxState().setVisible(true));
    }
}

How do I create JCheckBox component?

This example demonstrate a various way to create JCheckBox component. Here you can also see how the handle an event when the checkbox is clicked by user.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class CheckBoxDemoCreate extends JFrame {
    public CheckBoxDemoCreate() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JCheckBox Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Creating checkbox with text label
        JCheckBox checkBoxA = new JCheckBox("Selection A");

        // Creating checkbox with text label a set the state into checked
        JCheckBox checkBoxB = new JCheckBox("Selection B", true);

        // Creating checkbox with text label and a default unselected image icon
        ImageIcon icon = new ImageIcon(
                Objects.requireNonNull(
                        this.getClass().getResource("/images/lightbulb_off.png")));
        JCheckBox checkBoxC = new JCheckBox("Selection C", icon);
        // Add action listener to listen for click and change the image icon
        // respectively
        checkBoxC.addActionListener(e -> {
            JCheckBox checkBox = (JCheckBox) e.getSource();
            if (checkBox.isSelected()) {
                checkBox.setIcon(new ImageIcon(
                        Objects.requireNonNull(
                                this.getClass().getResource("/images/lightbulb.png"))));
                // Perform other actions here!
            } else {
                checkBox.setIcon(new ImageIcon(
                        Objects.requireNonNull(
                                this.getClass().getResource("/images/lightbulb_off.png"))));
                // Perform other actions here!
            }
        });

        getContentPane().add(checkBoxA);
        getContentPane().add(checkBoxB);
        getContentPane().add(checkBoxC);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new CheckBoxDemoCreate().setVisible(true));
    }
}
Swing JCheckBox Demo

Swing JCheckBox Demo