Radio Button in Java

Radio Button is a circular button on web page that can be selected by the user. The AWT Widget used to create a radio button is Checkbox. The Checkbox class is used by the AWT to create both Radio Button and Checkbox.

Radio Button in Java

Radio Button in Java

     

Radio Button is a circular button on web page that can be selected by the user. The AWT  Widget used to create a radio button is Checkbox. The Checkbox class is used by the AWT to create both Radio Button and Checkbox. There is no Radio Button class.awt to create a RadioButton.In order to create a RadioButton,we need to create a Checkbox first, followed by adding Radio Button in a Checkbox. The Swing  Class used to create a Radio Button is JRadioButton.

Let us take a Example that helps you in Understanding to create a Radio Button in a better way-

In This section we want to describe you a code that helps you in creating a Radio Button, For this we import a list of  package name like

 Checkbox, Checkbox Group- that contain the class definition for creating a Radio Button.

 Frame-AWT is used to create a container that provides you a separate window.

Grid Layout-This class Provide you to create  a rectangular grid in which various component of equal size dimension are placed.

Window Adapter-This class Provides you the list of action event taking place within a separate Window. The method used in this class is completely empty. 

Window Event-This Class Provides you a list of action event generated on clicking a window components.

Now After Understanding and importing all these Package, Lets us move to the code section, In this program code we have taken a class name 'RadioButtonTest' for creating a Checkbox Components A Checkbox is used to make a set of Checkbox group The Checkbox group button in  a Checkbox button can be made 'On' state at any time and can be made 'Off' state in the same way. We make an object of Frame in which Radio Button is passed as argument that is used to display the name of frame as 'RadioButtonGroup'.

Label( )-The constructor used to create an instance of label that is used to display the user defined message written by a programmer.

Checkbox Group( )-The constructor used to create an instance of checkbox group used further for displaying a Radio Button.

  
  import java.awt.Checkbox;
  import java.awt.CheckboxGroup;
  import java.awt.Frame;
  import java.awt.GridLayout;
  import java.awt.event.WindowAdapter;
  import java.awt.event.WindowEvent;
  import java.awt.Label;
   public class RadioButtonTest
 {
   public static void main(String[] args)
 {
  Frame f=new Frame("RadioButton Group");
  Label l1=new Label("What is your rating:");
  f setLayout(new GridLayout(0, 1));
  CheckboxGroup group=new CheckboxGroup();
  f.add(l1);
   f.add(new Checkbox("Excellent", group, true));
   f.add(new Checkbox("VeryGood", group, false));
   f.add(new Checkbox("Good", group,false));
   f.add(new Checkbox("Average", group, false));
   f.add(new Checkbox("Poor",group,false));
  f.setSize(250,200);
   f.setVisible(true);
   f.pack();
   f.addWindowListener(new WindowAdapter()
 {
  public void windowClosing(WindowEvent we)
 {
 System.exit(0);
 }
 });
 }
 }

Output on Command Prompt


C:\saurabh>javac RadioButtonTest.java

C:\saurabh>java RadioButtonTest

On execution of code ,the program create you a Radio Button name 'RadioButtonGroup' with label 'What is your rating' along with 5 different Radio Button set by the programmer code.

This will help you a lot in understanding a Radio Button If you have any further queries and doubt on this ,you can send your feedback and we will try our best to resolve your queries.