Put JCheckBox into group

Complete source code below will show you, how to create group for JCheckBox.

*************************************************************************
COMPLETE SOURCE CODE FOR : PutJCheckBoxIntoGroup.java
*************************************************************************


import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.ButtonGroup;

import java.awt.GridLayout;

public class PutJCheckBoxIntoGroup
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Put JCheckBox into group )
JFrame frame=new JFrame("Put JCheckBox into group");

//Create check box using JCheckBox
JCheckBox checkBox1=new JCheckBox("Duck");
JCheckBox checkBox2=new JCheckBox("Chicken");
JCheckBox checkBox3=new JCheckBox("Cow");
JCheckBox checkBox4=new JCheckBox("Sheep");

//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(4,1);

//Create button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

//Add all created check box into button group
bg.add(checkBox1);
bg.add(checkBox2);
bg.add(checkBox3);
bg.add(checkBox4);

//Set JFrame layout
frame.setLayout(gl);

//Add all created check box into JFrame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(checkBox4);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
frame.setVisible(true);
}
}


*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************

RELAXING NATURE VIDEO