«Back to Home

Core Java

Topics

JRadioButton Class In Java

JRadioButton Class
 
In Java, JRadioButton class creates a radio button and it is used to choose one option from the multiple options. It is commonly used in the exam systems or the quiz. It must be added in ButtonGroup to select one radio button only.
 
The JRadioButton class extends the JToggleButton class, which extends AbstractButton class.
 
Constructors of JRadioButton class

JRadioButton()

This constructor creates an unselected radio button with no text.
 
JRadioButton(String s)

This constructor creates a unselected radio button with the particular text.
 
JRadioButton(String s, boolean selected)

This constructor creates a radio button with the particular text and the selected status.
 
Methods of AbstractButton class

public void setText(String s)

This method is used to set the particular text on the button.
 
public String getText()

This method is used to return the text of the button.
 
public void setEnabled(boolean b)

This method is used to enable or disable the button.
 
public void setIcon(Icon b)

This method is used to set the particular Icon on the button.
 
public Icon getIcon()

This method is used to get the Icon of the button.
 
public void setMnemonic(int a)

This method is used to set the mnemonic on the button.
 
public void addActionListener(ActionListener a)

This method is used to add the action listener to the object.
 
Let’s see an example of JRadioButton class, given below.
 
Code
  1. import javax.swing.*;  
  2. public class JRadioButtonClass {  
  3.     JFrame jf;  
  4.     JRadioButtonClass() {  
  5.         jf = new JFrame();  
  6.         JRadioButton r1 = new JRadioButton("A) Yes");  
  7.         JRadioButton r2 = new JRadioButton("B) No");  
  8.         r1.setBounds(601507030);  
  9.         r2.setBounds(601507030);  
  10.         ButtonGroup bg = new ButtonGroup();  
  11.         bg.add(r1);  
  12.         bg.add(r2);  
  13.         jf.add(r1);  
  14.         jf.add(r2);  
  15.         jf.setSize(400400);  
  16.         jf.setLayout(null);  
  17.         jf.setVisible(true);  
  18.     }  
  19.     public static void main(String[] args) {  
  20.         new JRadioButtonClass();  
  21.     }  
  22. }  
31

ButtonGroup class

ButtonGroup class is used to group multiple buttons. Thus, at a time, only one button can be selected.
 
Let’s see an example, given below, JRadioButton class with the event handling.
 
Code
  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class JRadioButtonClass extends JFrame implements ActionListener {  
  4.     JRadioButton rb1, rb2;  
  5.     JButton b;  
  6.     JRadioButtonClass() {  
  7.         rb1 = new JRadioButton("Yes");  
  8.         rb1.setBounds(1005010030);  
  9.         rb2 = new JRadioButton("No");  
  10.         rb2.setBounds(10010010030);  
  11.         ButtonGroup bg = new ButtonGroup();  
  12.         bg.add(rb1);  
  13.         bg.add(rb2);  
  14.         b = new JButton("Enter");  
  15.         b.setBounds(1001508030);  
  16.         b.addActionListener(this);  
  17.         add(rb1);  
  18.         add(rb2);  
  19.         add(b);  
  20.         setSize(300300);  
  21.         setLayout(null);  
  22.         setVisible(true);  
  23.     }  
  24.     public void actionPerformed(ActionEvent e) {  
  25.         if (rb1.isSelected()) {  
  26.             JOptionPane.showMessageDialog(this"Welcome");  
  27.         }  
  28.         if (rb2.isSelected()) {  
  29.             JOptionPane.showMessageDialog(this"Unvalid");  
  30.         }  
  31.     }  
  32.     public static void main(String args[]) {  
  33.         new JRadioButtonClass();  
  34.     }  
  35. }  
32
33

Output

34

35
 
Summary

Thus, we learnt that JRadioButton class creates a radio button and it is used to choose one option from multiple options and also learnt how to use it in Java.