Java Program to Create Check Boxes and Radio Buttons

This is a Java Program to Create Check Boxes and Radio Buttons and Display the Selected in a Text Area

Problem Description

We have to write a program in Java such that it creates 3 Radio Buttons and 3 Check Boxes and displays the selected using two labels.

Expected Input and Output

To display the Radio Buttons and Check Boxes selected, we have the following sets of input and output.

1. When Radio Buttons are Selected:

Suppose there are 3 Radio Buttons namely Button 1, Button 2 and Button 3
If we select all 3 Radio Buttons,
then the expected output is "Button(s) Selected : Button 1, Button 2, Button 3"

2. When Check Boxes are Selected:

Suppose there are 3 Radio Buttons & 3 Check Boxes
Button 1, Button 2 and Button 3
Checkbox 1, Checkbox 2 and Checkbox 3
If we select all 3 Radio Buttons & 3 Check Boxes,
then the expected output is 
"Button(s) Selected : Button 1, Button 2, Button 3"
"CheckBox(s) Selected : Checkbox 1, Checkbox 2, Checkbox 3"

3. When Radio Buttons are De-Selected:

advertisement
advertisement
Suppose there are 3 Radio Buttons & 3 Check Boxes
Button 1, Button 2 and Button 3
Checkbox 1, Checkbox 2 and Checkbox 3
If we select 2 Radio Buttons (Button 1 & Button 3) & 3 Check Boxes,
then the expected output is 
"Button(s) Selected : Button 1, Button 3"
"CheckBox(s) Selected : Checkbox 1, Checkbox 2, Checkbox 3"

4. When Check Boxes are De-Selected:

Suppose there are 3 Radio Buttons & 3 Check Boxes
Button 1, Button 2 and Button 3
Checkbox 1, Checkbox 2 and Checkbox 3
If we select 2 Radio Buttons (Button 1 & Button 3) & 1 Check Box (Checkbox 2),
then the expected output is 
"Button(s) Selected : Button 1, Button 3"
"CheckBox(s) Selected : Checkbox 2"
Problem Solution

1. Create a frame with three Radio Buttons and three Check Boxes respectively.
2. Add ActionListener with the Radio Buttons and ItemListener with the Check Boxes.
3. Display the list of button(s) selected in label text1.
4. Display the list of CheckBox(s) selected in label text2.

Program/Source Code

Here is source code of the Java Program to display the Radio Buttons and Check Boxes selected. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to Display Radio Buttons and Check Boxes selected*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Button_Checkbox implements ActionListener,ItemListener
  6. {
  7.     static JFrame frame;
  8.     static JLabel text1,text2;
  9.     static JCheckBox[] checkbox;
  10.     static JRadioButton[] button;
  11.     //Driver function
  12.     public static void main(String args[])
  13.     {
  14.         //Create a frame
  15. 	frame=new JFrame("Buttons & Checkboxes");
  16. 	frame.setSize(600,600);
  17. 	frame.setLayout(null);
  18. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. 	frame.getContentPane().setBackground(Color.white);
  20. 	//Create the text fields
  21. 	text1=new JLabel("");
  22. 	text1.setBounds(0,450,600,50);
  23. 	frame.add(text1);
  24. 	text2=new JLabel("");
  25. 	text2.setBounds(0,500,600,50);
  26. 	frame.add(text2);
  27. 	//Create an object
  28. 	Button_Checkbox obj=new Button_Checkbox();
  29. 	//Create 3 buttons
  30. 	button=new JRadioButton[3];
  31. 	for(int i=0;i<3;i++)
  32. 	{
  33. 		button[i]=new JRadioButton("Button "+(i+1));
  34. 		button[i].setBounds(200,i*80,100,50);
  35. 		frame.add(button[i]);
  36. 		button[i].addActionListener(obj);
  37. 	}
  38. 	//Create 3 Checkbox
  39. 	checkbox=new JCheckBox[3];
  40. 	for(int i=0;i<3;i++)
  41. 	{
  42. 		checkbox[i]=new JCheckBox("Checkbox"+(i+1));
  43. 		checkbox[i].setBounds(220,(240)+i*80,100,50);
  44. 		frame.add(checkbox[i]);
  45. 		checkbox[i].addItemListener(obj);
  46. 	}
  47. 	//Display frame
  48. 	frame.setVisible(true);
  49.     }
  50.     //To display button selected
  51.     public void actionPerformed(ActionEvent e)
  52.     {
  53.         String s="";
  54. 	for(int i=0;i<3;i++)
  55. 	{
  56. 	    if(button[i].isSelected())
  57. 	        s=s+" "+button[i].getText();
  58. 	}
  59. 	text1.setText("Button(s) Selected : "+" "+s);
  60.     }
  61.     //To display the checkboxes checked
  62.     public void itemStateChanged(ItemEvent e)
  63.     {
  64.         String s="";
  65. 	for(int i=0;i<3;i++)
  66. 	{	
  67. 	    if(checkbox[i].isSelected())
  68. 	        s=s+" "+checkbox[i].getText();				
  69. 	}
  70. 	text2.setText("Checkbox(s) Selected : "+s);	
  71.     }
  72. }
Program Explanation

1. A frame is created with three Radio Buttons and Check Boxes.
2. Function isSelected is used to get the buttons/checkboxes selected.
3. Appropriate messages is displayed in the labels.

advertisement
Runtime Test Cases

Here’s the run time test cases to display the Radio Buttons and Check Boxes selected.

Test case 1 – To display the Radio Buttons Selected.
java-program-display-selected-button-selected

Test case 2 – To display the Check Boxes Selected.
java-program-display-selected-checkbox-selected

Test case 3 – To display the Radio Buttons after De-Selected.
java-program-display-selected-button-deselected

advertisement

Test case 4 – To display the Check Boxes after De-Selected.
java-program-display-selected-checkbox-deselected

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.