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

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *