Swing Checkbox ItemListener Java Example

Swing – Checkbox ItemListener Java

In this example, We will show you a Checkbox ItemListener Java program. This example program has been tested and shared in the same post.

Sample Program

package com.dineshkrish;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class CheckboxExample extends JFrame {

	private JPanel panel;

	private JCheckBox school;
	private JCheckBox ug;
	private JCheckBox pg;
	private JCheckBox phd;

	private JLabel label;

	public CheckboxExample(String title) {

		// setting properties for windows
		super.setTitle(title);
		super.setDefaultCloseOperation(EXIT_ON_CLOSE);
		super.setSize(300, 100);
		super.setVisible(true);
		super.setLayout(new BorderLayout());

		init();

	}

	private void init() {

		// creating panel
		panel = new JPanel();

		// creating all checkbox
		school = new JCheckBox("School");
		ug = new JCheckBox("UG");
		pg = new JCheckBox("PG");
		phd = new JCheckBox("Phd");

		school.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {

				populateSelectedValue();
			}
		});

		ug.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {

				populateSelectedValue();
			}
		});

		pg.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {

				populateSelectedValue();
			}
		});

		phd.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {

				populateSelectedValue();
			}
		});

		// adding all checkbox to panel
		panel.add(school);
		panel.add(ug);
		panel.add(pg);
		panel.add(phd);

		label = new JLabel();

		// adding panel to JFrame
		add(panel, BorderLayout.NORTH);
		add(label, BorderLayout.SOUTH);

	}

	private void populateSelectedValue() {

		List<String> selectedValue = new ArrayList<String>();

		if (school.isSelected()) {

			selectedValue.add(school.getActionCommand());
		} else {

			selectedValue.remove(school.getActionCommand());
		}

		if (ug.isSelected()) {

			selectedValue.add(ug.getActionCommand());
		} else {

			selectedValue.remove(ug.getActionCommand());
		}

		if (pg.isSelected()) {

			selectedValue.add(pg.getActionCommand());
		} else {

			selectedValue.remove(pg.getActionCommand());
		}

		if (phd.isSelected()) {

			selectedValue.add(phd.getActionCommand());
		} else {

			selectedValue.remove(phd.getActionCommand());
		}

		label.setText("Your have completed : " + selectedValue);

	}

	public static void main(String[] args) {

		new CheckboxExample("Checkbox Example");
	}
}

Output

Swing Checkbox ItemListener Java Example

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.