Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Add JRadioButton into JFrame (Java)

Source code below will show you, how to create radio button in java using JRadioButton. It also show you how to use ButtonGroup that enable only one selection can make at radio button in one time.

*******************************************************************
COMPLETE SOURCE CODE FOR : AddJRadioButtonIntoJFrame.java
*******************************************************************
import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;

import java.awt.FlowLayout;
import java.awt.GridLayout;

public class AddJRadioButtonIntoJFrame
{
public static void main(String[]args)
{
 //Create JRadioButton with text(Chicken)
 JRadioButton radioButton1=new JRadioButton("Chicken");

 //Create JRadioButton with text(Duck)
 JRadioButton radioButton2=new JRadioButton("Duck");

 //Create JRadioButton with text(Cow)
 JRadioButton radioButton3=new JRadioButton("Cow");

 //Create JRadioButton with text(Horse)
 JRadioButton radioButton4=new JRadioButton("Horse");

 //Create JRadioButton with text(Sheep)
 JRadioButton radioButton5=new JRadioButton("Sheep");

 //Create JRadioButton with text(Chicken)
 JRadioButton radioButton6=new JRadioButton("Chicken");

 //Create JRadioButton with text(Duck)
 JRadioButton radioButton7=new JRadioButton("Duck");

 //Create JRadioButton with text(Cow)
 JRadioButton radioButton8=new JRadioButton("Cow");

 //Create JRadioButton with text(Horse)
 JRadioButton radioButton9=new JRadioButton("Horse");

 //Create JRadioButton with text(Sheep)
 JRadioButton radioButton10=new JRadioButton("Sheep");

 //Create button group will make at one time
 //only one JRadioButton can select in that group.
 //Create first JRadioButton group
 ButtonGroup groupFirst=new ButtonGroup();
 groupFirst.add(radioButton1);
 groupFirst.add(radioButton2);
 groupFirst.add(radioButton3);
 groupFirst.add(radioButton4);
 groupFirst.add(radioButton5);

 //Create second JRadioButton group
 ButtonGroup groupSecond=new ButtonGroup();
 groupSecond.add(radioButton6);
 groupSecond.add(radioButton7);
 groupSecond.add(radioButton8);
 groupSecond.add(radioButton9);
 groupSecond.add(radioButton10);

 //Create a JFrame with title ( Add JRadioButton into JFrame )
 JFrame frame=new JFrame("Add JRadioButton into JFrame");

 //Create a JPanel that we use to add JRadioButton into it
 JPanel panel=new JPanel();

 //Create a JPanel that we use to add JRadioButton into it
 JPanel panel2=new JPanel();

 //Set layout for JFrame
 frame.setLayout(new FlowLayout());

 //Set layout for first JPanel
 panel.setLayout(new FlowLayout());

 //Set layout for second JPanel
 panel2.setLayout(new GridLayout(5,1));

 //Add radioButton1 - radioButton5 into first JPanel
 panel.add(radioButton1);
 panel.add(radioButton2);
 panel.add(radioButton3);
 panel.add(radioButton4);
 panel.add(radioButton5);

 //Add radioButton6 - radioButton10 into second JPanel
 panel2.add(radioButton6);
 panel2.add(radioButton7);
 panel2.add(radioButton8);
 panel2.add(radioButton9);
 panel2.add(radioButton10);

 //Add first JPanel into JFrame
 frame.add(panel);

 //Add second JPanel into JFrame
 frame.add(panel2);

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

 //Set size for JFrame
 frame.setSize(400,400);

 //Make JFrame visible. So we can see it
 frame.setVisible(true);
}
}


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