This is a Java program to get and set state and get label of a checkbox.
We have to write a program in Java such that it creates a checkbox to get its present state, change/set its state and get its label.
For Get and Set State and Get Label of a CheckBox, we can have the following 5 different sets of input and output.
1. To get the un-checked state:
For example:
If the checkbox is in un-checked state, the expected output is "Status of Checkbox : Un-Checked"
2. To get the checked state:
For example:
If the checkbox is in checked state, the expected output is "Status of Checkbox : Checked"
3. To get the set state from un-checked to checked:
For example:
If the state of checkbox is changed from un-checked to checked, the expected output is "Status of Checkbox Changed from Un-Checked to Checked"
4. To get the set state from checked to un-checked:
For example:
If the state of checkbox is changed from checked to un-checked, the expected output is "Status of Checkbox Changed from Checked to Un-Checked"
5. To get the label of checkbox:
For example:
If the get label option is selected, the expected output is "Label of Checkbox : Sample Checkbox"
1. Create a frame, a text field, 3 buttons – Get State, Set State and Get Label and a Checkbox.
2. Associate ActionListener with the 3 buttons.
a) Method get_State is called when the Get State button is clicked.
b) Method set_State is called when the Set State button is clicked.
c) Method get_Label is called when the Get Label button is clicked.
3. Use function getState() to get the present state of the checkbox.
4. Use function setState() to set the state of the checkbox.
5. Use function getLabel() to get the label of the checkbox.
6. Display appropriate message in the text fields.
Here is source code of the Java Program to Get and Set State and Get Label of a CheckBox. The program is successfully compiled and tested using javac compiler on Fedora 30.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Check_Box implements ActionListener
{
static JTextField text;
static Checkbox checkbox;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("Check Box");
frame.setSize(500,500);
frame.setBackground(Color.white);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a textfield
text=new JTextField();
text.setBounds(0,0,500,50);
frame.add(text);
//Create 3 buttons
JButton get_st=new JButton("Get State");
JButton set_st=new JButton("Set State");
JButton get_lb=new JButton("Get Label");
get_st.setBounds(50,80,100,50);
set_st.setBounds(180,80,100,50);
get_lb.setBounds(310,80,100,50);
frame.add(get_st);
frame.add(set_st);
frame.add(get_lb);
//Create an object of class
Check_Box obj=new Check_Box();
//Associate ActionListener with the buttons
get_st.addActionListener(obj);
set_st.addActionListener(obj);
get_lb.addActionListener(obj);
//Create a checkbox
checkbox=new Checkbox("Sample Checkbox");
checkbox.setBounds(150,200,200,80);
frame.add(checkbox);
//Display the frame
frame.setVisible(true);
}
//function to call different methods based on user's choice of button
@Override
public void actionPerformed(ActionEvent e)
{
String option=e.getActionCommand();
if(option.equals("Get State"))
get_State();
else if(option.equals("Set State"))
set_State();
else
get_Label();
}
//function to get the current state of the checkbox
public void get_State()
{
boolean state=checkbox.getState();
if(state==true)
text.setText("State of Checkbox : Checked");
else
text.setText("State of Checkbox : Un-Checked");
}
//function to change the current state of the checkbox
public void set_State()
{
text.setText("State of Checkbox changed from ");
boolean state=checkbox.getState();
if(state==true)
{
checkbox.setState(false);
text.setText(text.getText()+"Checked to Un-Checked");
}
else
{
checkbox.setState(true);
text.setText(text.getText()+"Un-Checked to Checked");
}
}
//function to get the label of the checkbox
public void get_Label()
{
text.setText("Label of the checkbox is : ");
text.setText(text.getText()+checkbox.getLabel());
}
}
1. The actionPerformed(ActionEvent) is called when any of the buttons is clicked. Use function getActionCommand() to know which button was clicked and call the respective methods.
2. In method get_State() display the present state of the checkbox.
3. In method set_State() change the current state of checkbox and display the necessary message.
4. In method get_Label() display the label of the checkbox.
Here’s the run time test cases for Get and Set State and Get Label of a CheckBox for 5 different input cases.
Test case 1 – Here’s the runtime output to get the un-checked state. For example, the checkbox is in un-checked state, then the program displays “Status of Checkbox : Un-Checked”.
Test case 2 – Here’s the runtime output to get the checked state. If user selects the checkbox, then the program displays “Status of Checkbox : Checked”.
Test case 3 – Here’s the runtime output to set the state from un-checked to checked. If we change the state of checkbox from un-checked to checked, then the program displays “Status of Checkbox Changed from Un-Checked to Checked”.
Test case 4 – Here’s the runtime output to set the state from checked to un-checked. If we change the state of checkbox from checked to un-checked, then the program displays “Status of Checkbox Changed from Checked to Un-Checked”
Test case 5 – Here’s the runtime output to get the label of the checkbox. If user selects the get label option, then the program displays “Label of Checkbox : Sample Checkbox”.
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Apply for Computer Science Internship
- Check Java Books
- Apply for Java Internship
- Practice Programming MCQs
- Practice BCA MCQs