We use the JRadioButton class to create a radio button. Radio button is use to select one option from multiple options. It is used in filling forms, online objective papers and quiz.
We add radio buttons in a ButtonGroup so that we can select only one radio button at a time. We use “ButtonGroup” class to create a ButtonGroup and add radio button in a group.
Methods Used :
- JRadioButton() : Creates a unselected RadioButton with no text.
Example:JRadioButton j1 = new JRadioButton()
- JButton(String s) : Creates a JButton with a specific text.
Example:JButton b1 = new JButton("Button")
- JLabel(String s) : Creates a JLabel with a specific text.
Example:JLabel L = new JLabel("Label 1")
- ButtonGroup() : Use to create a group, in which we can add JRadioButton. We can select only one JRadioButton in a ButtonGroup.
Steps to Group the radio buttons together.- Create a ButtonGroup instance by using “ButtonGroup()” Method.
ButtonGroup G = new ButtonGroup()
- Now add buttons in a Group “G”, with the help of “add()” Method.
Example:
G.add(Button1); G.add(Button2);
- Create a ButtonGroup instance by using “ButtonGroup()” Method.
- isSelected() : it will return a Boolean value true or false, if a JRadioButton is selected it Will return true otherwise false.
Example:
JRadioButton.isSelected()
- Set(…) and Get(…) Methods :
i) Set and get are used to replace directly accessing member variables from external classes.ii) Instead of accessing class member variables directly, you define get methods to access these variables, and set methods to modify them.
Description of some functions used in program, is given in this link : Functions Description
Program 1 : JRadioButton Without ActionListener
// Java program to show JRadioButton Example. // in java. Importing different Package. import java.awt.*; import javax.swing.*; import java.awt.event.*; class Demo extends JFrame { // Declaration of object of JRadioButton class. JRadioButton jRadioButton1; // Declaration of object of JRadioButton class. JRadioButton jRadioButton2; // Declaration of object of JButton class. JButton jButton; // Declaration of object of ButtonGroup class. ButtonGroup G1; // Declaration of object of JLabel class. JLabel L1; // Constructor of Demo class. public Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JRadioButton" class. jRadioButton1 = new JRadioButton(); // Initialization of object of "JRadioButton" class. jRadioButton2 = new JRadioButton(); // Initialization of object of "JButton" class. jButton = new JButton( "Click" ); // Initialization of object of "ButtonGroup" class. G1 = new ButtonGroup(); // Initialization of object of " JLabel" class. L1 = new JLabel( "Qualification" ); // setText(...) function is used to set text of radio button. // Setting text of "jRadioButton2". jRadioButton1.setText( "Under-Graduate" ); // Setting text of "jRadioButton4". jRadioButton2.setText( "Graduate" ); // Setting Bounds of "jRadioButton2". jRadioButton1.setBounds( 120 , 30 , 120 , 50 ); // Setting Bounds of "jRadioButton4". jRadioButton2.setBounds( 250 , 30 , 80 , 50 ); // Setting Bounds of "jButton". jButton.setBounds( 125 , 90 , 80 , 30 ); // Setting Bounds of JLabel "L2". L1.setBounds( 20 , 30 , 150 , 50 ); // "this" keyword in java refers to current object. // Adding "jRadioButton2" on JFrame. this .add(jRadioButton1); // Adding "jRadioButton4" on JFrame. this .add(jRadioButton2); // Adding "jButton" on JFrame. this .add(jButton); // Adding JLabel "L2" on JFrame. this .add(L1); // Adding "jRadioButton1" and "jRadioButton3" in a Button Group "G2". G1.add(jRadioButton1); G1.add(jRadioButton2); } } class RadioButton { // Driver code. public static void main(String args[]) { // Creating object of demo class. Demo f = new Demo(); // Setting Bounds of JFrame. f.setBounds( 100 , 100 , 400 , 200 ); // Setting Title of frame. f.setTitle( "RadioButtons" ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
Output:
Program 2 : JRadioButton With ActionListener
// Java program to show JRadioButton Example. // in java. Importing different Package. import java.awt.*; import javax.swing.*; import java.awt.event.*; class Demo extends JFrame { // Declaration of object of JRadioButton class. JRadioButton jRadioButton1; // Declaration of object of JRadioButton class. JRadioButton jRadioButton2; // Declaration of object of JButton class. JButton jButton; // Declaration of object of ButtonGroup class. ButtonGroup G1; // Declaration of object of JLabel class. JLabel L1; // Constructor of Demo class. public Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JRadioButton" class. jRadioButton1 = new JRadioButton(); // Initialization of object of "JRadioButton" class. jRadioButton2 = new JRadioButton(); // Initialization of object of "JButton" class. jButton = new JButton( "Click" ); // Initialization of object of "ButtonGroup" class. G1 = new ButtonGroup(); // Initialization of object of " JLabel" class. L1 = new JLabel( "Qualification" ); // setText(...) function is used to set text of radio button. // Setting text of "jRadioButton2". jRadioButton1.setText( "Under-Graduate" ); // Setting text of "jRadioButton4". jRadioButton2.setText( "Graduate" ); // Setting Bounds of "jRadioButton2". jRadioButton1.setBounds( 120 , 30 , 120 , 50 ); // Setting Bounds of "jRadioButton4". jRadioButton2.setBounds( 250 , 30 , 80 , 50 ); // Setting Bounds of "jButton". jButton.setBounds( 125 , 90 , 80 , 30 ); // Setting Bounds of JLabel "L2". L1.setBounds( 20 , 30 , 150 , 50 ); // "this" keyword in java refers to current object. // Adding "jRadioButton2" on JFrame. this .add(jRadioButton1); // Adding "jRadioButton4" on JFrame. this .add(jRadioButton2); // Adding "jButton" on JFrame. this .add(jButton); // Adding JLabel "L2" on JFrame. this .add(L1); // Adding "jRadioButton1" and "jRadioButton3" in a Button Group "G2". G1.add(jRadioButton1); G1.add(jRadioButton2); // Adding Listener to JButton. jButton.addActionListener( new ActionListener() { // Anonymous class. public void actionPerformed(ActionEvent e) { // Override Method // Declaration of String class Objects. String qual = " " ; // If condition to check if jRadioButton2 is selected. if (jRadioButton1.isSelected()) { qual = "Under-Graduate" ; } else if (jRadioButton2.isSelected()) { qual = "Graduate" ; } else { qual = "NO Button selected" ; } // MessageDialog to show information selected radio buttons. JOptionPane.showMessageDialog(Demo. this , qual); } }); } } class RadioButton { // Driver code. public static void main(String args[]) { // Creating object of demo class. Demo f = new Demo(); // Setting Bounds of JFrame. f.setBounds( 100 , 100 , 400 , 200 ); // Setting Title of frame. f.setTitle( "RadioButtons" ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
Output:
After pressing Button “click”.
Program 3 Program to create a simple group of radio buttons (with image )and add item listener to them
// Java Program to create a simple group of radio buttons // (with image )and add item listener to them import java.awt.event.*; import java.awt.*; import javax.swing.*; class solve extends JFrame implements ItemListener { // frame static JFrame f; // radiobuttons static JRadioButton b, b1; // create a label static JLabel l1; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object solve s = new solve(); // create a panel JPanel p = new JPanel(); // create a new label JLabel l = new JLabel( "which website do you like?" ); l1 = new JLabel( "neveropen selected" ); // create Radio buttons b = new JRadioButton( "neveropen" , new ImageIcon( "f:/gfg.jpg" )); b1 = new JRadioButton( "others" ); // create a button group ButtonGroup bg = new ButtonGroup(); // add item listener b.addItemListener(s); b1.addItemListener(s); // add radio buttons to button group bg.add(b); bg.add(b1); b.setSelected( true ); // add button and label to panel p.add(l); p.add(b); p.add(b1); p.add(l1); f.add(p); // set the size of frame f.setSize( 400 , 400 ); f.show(); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == b) { if (e.getStateChange() == 1 ) { l1.setText( "neveropen selected" ); } } else { if (e.getStateChange() == 1 ) { l1.setText( "others selected" ); } } } } |
Output :
Note : The following programs might not run in an online Compiler please use an Offline IDE.