Put JRadioButton into group

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

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


import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;

import java.awt.GridLayout;

public class PutJRadioButtonIntoGroup
{
public static void main(String[]args)
{
//Create five radio button that will be put into a group
JRadioButton firstRadioButton=new JRadioButton("First radio button");
JRadioButton secondRadioButton=new JRadioButton("Second radio button");
JRadioButton thirdRadioButton=new JRadioButton("Third radio button");
JRadioButton fourthRadioButton=new JRadioButton("Fourth radio button");
JRadioButton fifthRadioButton=new JRadioButton("Fifth radio button");

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

//Add all radio button into created group
bg.add(firstRadioButton);
bg.add(secondRadioButton);
bg.add(thirdRadioButton);
bg.add(fourthRadioButton);
bg.add(fifthRadioButton);

//Create a window using JFrame with title ( Put JRadioButton into group )
JFrame frame=new JFrame("Put JRadioButton into group");

//Set JFrame layout to grid layout
frame.setLayout(new GridLayout(5,1));

//Add all created button into group
frame.add(firstRadioButton);
frame.add(secondRadioButton);
frame.add(thirdRadioButton);
frame.add(fourthRadioButton);
frame.add(fifthRadioButton);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(300,150);

//Make JFrame visible
frame.setVisible(true);
}
}


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

RELAXING NATURE VIDEO