This is a Java Program to Create Check Boxes and Radio Buttons and Display the Selected in a Text Area
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.
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:
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"
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.
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.
/*Java Program to Display Radio Buttons and Check Boxes selected*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Button_Checkbox implements ActionListener,ItemListener
{
static JFrame frame;
static JLabel text1,text2;
static JCheckBox[] checkbox;
static JRadioButton[] button;
//Driver function
public static void main(String args[])
{
//Create a frame
frame=new JFrame("Buttons & Checkboxes");
frame.setSize(600,600);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.white);
//Create the text fields
text1=new JLabel("");
text1.setBounds(0,450,600,50);
frame.add(text1);
text2=new JLabel("");
text2.setBounds(0,500,600,50);
frame.add(text2);
//Create an object
Button_Checkbox obj=new Button_Checkbox();
//Create 3 buttons
button=new JRadioButton[3];
for(int i=0;i<3;i++)
{
button[i]=new JRadioButton("Button "+(i+1));
button[i].setBounds(200,i*80,100,50);
frame.add(button[i]);
button[i].addActionListener(obj);
}
//Create 3 Checkbox
checkbox=new JCheckBox[3];
for(int i=0;i<3;i++)
{
checkbox[i]=new JCheckBox("Checkbox"+(i+1));
checkbox[i].setBounds(220,(240)+i*80,100,50);
frame.add(checkbox[i]);
checkbox[i].addItemListener(obj);
}
//Display frame
frame.setVisible(true);
}
//To display button selected
public void actionPerformed(ActionEvent e)
{
String s="";
for(int i=0;i<3;i++)
{
if(button[i].isSelected())
s=s+" "+button[i].getText();
}
text1.setText("Button(s) Selected : "+" "+s);
}
//To display the checkboxes checked
public void itemStateChanged(ItemEvent e)
{
String s="";
for(int i=0;i<3;i++)
{
if(checkbox[i].isSelected())
s=s+" "+checkbox[i].getText();
}
text2.setText("Checkbox(s) Selected : "+s);
}
}
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.
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.
Test case 2 – To display the Check Boxes Selected.
Test case 3 – To display the Radio Buttons after De-Selected.
Test case 4 – To display the Check Boxes after De-Selected.
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Practice Programming MCQs
- Check Java Books
- Practice BCA MCQs
- Apply for Computer Science Internship
- Practice Information Technology MCQs