Creating a JCheckBoxGroup


JCheckBoxGroup.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class JCheckBoxGroup extends JFrame{
   private ButtonGroup checkBoxGroup;
   private JCheckBox check1, check2, check3;
   private JPanel panel;
 
   public JCheckBoxGroup() {
      super("JCheckBoxGroup Example");
      panel = new JPanel(new GridLayout(3,0));
      check1 = new JCheckBox("1", true);
      check2 = new JCheckBox("2", false);
      check3 = new JCheckBox("3", false);
      checkBoxGroup = new ButtonGroup();
 
      //add CheckBoxes to ButtonGroup
      checkBoxGroup.add(check1);
      checkBoxGroup.add(check2);
      checkBoxGroup.add(check3);
  		
      panel.add(check1);
      panel.add(check2);
      panel.add(check3);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            dispose();
            System.exit(0);
         }
      });
   
      setContentPane(panel);
      setSize(250,150);
      setVisible(true);
   }
 
   public static void main(String args[]) {
      JCheckBoxGroup j = new JCheckBoxGroup();
   }
}