Java Program to Create 2 Radio Buttons ‘Yes’ and ‘No’ and Display the Selected Button Label

«
»

This is a Java program to create 2 radio buttons ‘yes’ and ‘no’ and display the selected button label.

Problem Description

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.

Expected Input and Output

For getting the label of 2 radio buttons, we can have the following sets of input and output.

advertisement

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"
Problem Solution

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.

advertisement
Program/Source Code

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.

  1. /*Java Program to Create Two Radio Buttons and Get the Label of Button Selected*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Radio_Button implements ActionListener
  6. {
  7. 	static JRadioButton yes;
  8. 	static JRadioButton no;
  9. 	static JTextField text;
  10. 	//Driver function
  11. 	public static void main(String args[])
  12. 	{
  13. 		//Create a frame
  14. 		JFrame frame=new JFrame("Radio Button");
  15. 		frame.setSize(500,500);
  16. 		frame.setLayout(null);
  17. 		frame.setBackground(Color.white);
  18. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. 		//Create a text field
  20. 		text=new JTextField();
  21. 		text.setBounds(0,0,500,50);
  22. 		frame.add(text);
  23. 		//Create two radio buttons
  24. 		yes=new JRadioButton("Yes");
  25. 		no=new JRadioButton("No");
  26. 		yes.setBounds(210,100,80,60);
  27. 		no.setBounds(210,200,80,60);
  28. 		frame.add(yes);
  29. 		frame.add(no);
  30. 		//Create an object of the class
  31. 		Radio_Button obj=new Radio_Button();
  32. 		//Associate ActionListener with the radio buttons
  33. 		yes.addActionListener(obj);
  34. 		no.addActionListener(obj);
  35. 		//Display the frame
  36. 		frame.setVisible(true);
  37. 	}
  38. 	//function to display the label of button selected
  39. 	public void actionPerformed(ActionEvent e)
  40. 	{
  41. 		String b=e.getActionCommand();		
  42. 		if(b.equals("Yes"))
  43. 		{
  44. 			if(no.isSelected())
  45. 				no.setSelected(false);
  46. 		}
  47. 		else
  48. 		{
  49. 			if(yes.isSelected())
  50. 				yes.setSelected(false);
  51. 		}
  52. 		text.setText("Label of Button Selected : "+b);
  53. 	}
  54. }
Program Explanation

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.

advertisement
Runtime Test Cases

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”.
java-program-radio-button-label-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”.
java-program-radio-button-label-no

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn