This is a Java program to create 2 radio buttons ‘yes’ and ‘no’ and display the selected button label.
We have to write a program in Java such that it creates 2 Radio Buttons – ‘Yes’ and ‘No’ and displays the label of the button selected in a TextField.
For getting the label of 2 radio buttons, we can have the following sets of input and output.
1. To get the label of ‘Yes’ button:
When the radio button 'Yes' is selected, then the expected output is "Label of Button Selected : Yes"
2. To get the label of ‘No’ button:
When the radio button 'No' is selected, then the expected output is "Label of Button Selected : No"
1. Create a frame, a text field, and two radio buttons (Yes & No).
2. Associate ActionListener with the two radio buttons.
3. In method actionPerformed when a radio button is selected check the status of the other radio button and change it to un-selected as two radio buttons cannot be selected at any time.
4. Display the message accordingly.
Here is source code of the Java Program to create two radio buttons and get the label of button selected. The program is successfully compiled and tested using javac compiler on Fedora 30.
/*Java Program to Create Two Radio Buttons and Get the Label of Button Selected*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Radio_Button implements ActionListener
{
static JRadioButton yes;
static JRadioButton no;
static JTextField text;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("Radio Button");
frame.setSize(500,500);
frame.setLayout(null);
frame.setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a text field
text=new JTextField();
text.setBounds(0,0,500,50);
frame.add(text);
//Create two radio buttons
yes=new JRadioButton("Yes");
no=new JRadioButton("No");
yes.setBounds(210,100,80,60);
no.setBounds(210,200,80,60);
frame.add(yes);
frame.add(no);
//Create an object of the class
Radio_Button obj=new Radio_Button();
//Associate ActionListener with the radio buttons
yes.addActionListener(obj);
no.addActionListener(obj);
//Display the frame
frame.setVisible(true);
}
//function to display the label of button selected
public void actionPerformed(ActionEvent e)
{
String b=e.getActionCommand();
if(b.equals("Yes"))
{
if(no.isSelected())
no.setSelected(false);
}
else
{
if(yes.isSelected())
yes.setSelected(false);
}
text.setText("Label of Button Selected : "+b);
}
}
1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
2. Create an text field with setBounds(int x, int y, int width, int height), where ‘x’ & ‘y’ is used to set the position of top-left corner. ‘int width’ & ‘int height’ is used to set the width and height of the exit button.
3. Here the text field values of set bounds are text.setBounds(0,0,500,50);, where ‘0’ & ‘0’ is used to set the position of button from top and left side of the frame. ‘500’ & ’50’ is used to set the width and height of the exit button.
4. Create two radio buttons Yes & No by setting setbound values as follows:
yes.setBounds(210,100,80,60); -> Values to create Yes Radio Button
no.setBounds(210,200,80,60); -> Values to create No Radio Button
5. Radio_Button obj=new Radio_Button(); is used to create an object of the class.
6. Once object of the class is created. Associate ActionListener with the two radio buttons by using yes.addActionListener(obj); and no.addActionListener(obj);
7. frame.setVisible(true); is used to display the frame.
8. In method actionPerformed when a radio button is selected check the status of the other radio button and change it to un-selected as two radio buttons cannot be selected at any time. ActionEvent class is used to perform component defined action.
9. To check which button was clicked, and change the status of the other radio button to un-selected if it was selected using functions isSelected and setSelected respectively.
10. If any radio button is selected. Display appropriate message in the text box.
Here’s the run time test cases for getting the label of the radio button selected.
Test case 1 – Here’s the runtime output to get the label of ‘Yes’ button. If user selects the radio button ‘Yes’, then the program displays “Label of Button Selected : Yes”.
Test case 2 – Here’s the runtime output to get the label of ‘No’ button. If user selects the radio button ‘No’, then the program displays “Label of Button Selected : No”.
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice BCA MCQs
- Apply for Java Internship
- Apply for Computer Science Internship
- Check Programming Books
- Practice Information Technology MCQs