Java Swing GUI

JRadioButton – Java Swing – Example

In this tutorial, we are going to see an example of JRadioButton in Java Swing. We use the JRadioButton class to create a radio button. The radio button is used to select one of several options. It is used to fill in forms, online documents, and MCQs.

We add radio buttons to a group so that only one radio button can be selected at a time. We use the “ButtonGroup” class to create a button group and add a radio button to a group.


 
 

JRadioButton constructors class:
Constructeur de JRadioButton
Description
JRadioButton() Creates an unselected radio button without text.
JRadioButton(String s) Creates an unselected radio button with the specified text.
JRadioButton(String s, boolean selected) Creates a radio button with the specified and selected text.

 

Commonly used methods of JRadioButton class
  • void setText(String s) : It is used to set the text on the button.
  • String getText() : It is used to return the text of the button.
  • void setEnabled(boolean b) : It is used to enable or disable the button.
  • void setIcon(Icon b) : It is used to set the icon on the button.
  • Icon getIcon() : It is used to get the button icon.
  • void setMnemonic(int a) : It is used to set the mnemonic on the button.
  • void addActionListener(ActionListener a) : It is used to add action listener to this object.
 

Example 1 of JRadioButton in Java Swing:
import javax.swing.*;  

public class RadioButtonTest
{
  JFrame frame;  
  RadioButtonTest()
  {
    frame = new JFrame();  
    // Create the label 
    JLabel label = new JLabel("1 - Give the abbreviation of AWT?", JLabel.CENTER);
    label.setBounds(20,0,200,80);  
    
    // Create the radio buttons
    JRadioButton btn1 = new JRadioButton("A) Applet Windowing Toolkit");
    JRadioButton btn2 = new JRadioButton("B) Abstract Windowing Toolkit");
    JRadioButton btn3 = new JRadioButton("C) Absolute Windowing Toolkit");  
    
    // Set the position of the radio buttons
    btn1.setBounds(40,60,200,50);  
    btn2.setBounds(40,100,200,50);  
    btn3.setBounds(40,140,200,50); 
    
    // Add radio buttons to group
    ButtonGroup bg = new ButtonGroup();  
    bg.add(btn1);
    bg.add(btn2);  
    bg.add(btn3);  
    
    // Add buttons to frame
    frame.add(label);
    frame.add(btn1);
    frame.add(btn2);   
    frame.add(btn3);   
    
    frame.setSize(300,300);  
    frame.setLayout(null);  
    frame.setVisible(true);  
  }  
  public static void main(String[] args) {  
    new RadioButtonTest();  
  }  
}

Output:


 
 

Example 2 of JRadioButton with ActionListener in Java Swing:
import javax.swing.*;  
import java.awt.event.*;  

public class RadioButtonTest extends JFrame implements ActionListener
{
  JFrame frame;  
  JButton btn;
  JRadioButton rBtn1, rBtn2, rBtn3;
  
  RadioButtonTest()
  {
    frame = new JFrame();  
    // Create the label     
    JLabel label = new JLabel("1 - Give the abbreviation of AWT?", JLabel.CENTER);
    label.setBounds(20,0,200,80);  
    
    // Create the radio buttons
    rBtn1 = new JRadioButton("A) Applet Windowing Toolkit");
    rBtn2 = new JRadioButton("B) Absolute Windowing Toolkit");
    rBtn3 = new JRadioButton("C) Abstract Windowing Toolkit");  
    
    // Set the position of the radio buttons
    rBtn1.setBounds(40,60,200,50);  
    rBtn2.setBounds(40,100,200,50);  
    rBtn3.setBounds(40,140,200,50); 

    // Add radio buttons to group
    ButtonGroup bg = new ButtonGroup();  
    bg.add(rBtn1);
    bg.add(rBtn2);  
    bg.add(rBtn3);  

    btn = new JButton("Check");  
    btn.setBounds(100,200,80,30);  
    btn.addActionListener(this);  

    // Add buttons to frame
    frame.add(label);
    frame.add(rBtn1);
    frame.add(rBtn2);   
    frame.add(rBtn3);   
    frame.add(btn); 

    frame.setSize(300,300);  
    frame.setLayout(null);  
    frame.setVisible(true);  
  }  
  
  public void actionPerformed(ActionEvent e){
    if(rBtn1.isSelected() || rBtn2.isSelected()){  
      JOptionPane.showMessageDialog(this,"Your answer is wrong.");  
    }  
    if(rBtn3.isSelected()){  
      JOptionPane.showMessageDialog(this,"Your answer is correct.");  
    }  
  }  

  public static void main(String[] args) {  
    new RadioButtonTest();  
  }  
}

Output:


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *