How to Create a Checkbox using Java

Swing – Create a Checkbox using Java

In this example, We will show you a simple Swing program about, How to create a Checkbox using Java. This example program has been tested and shared with the output in the same post.

Sample Program

package com.dineshkrish;

import java.awt.BorderLayout;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
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;

	public CheckboxExample(String title) {

		// setting properties for windows
		super.setTitle(title);
		super.setDefaultCloseOperation(EXIT_ON_CLOSE);
		super.setSize(500, 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");

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

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

	}

	public static void main(String[] args) {

		new CheckboxExample("Checkbox Example");
	}
}

Output

How to Create a Checkbox using Java

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.