Java Program to Get and Set State and Get Label of a CheckBox

«
»

This is a Java program to get and set state and get label of a checkbox.

Problem Description

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.

Expected Input and Output

For Get and Set State and Get Label of a CheckBox, we can have the following 5 different sets of input and output.

advertisement

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:

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

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.

advertisement

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.

Program/Source Code

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.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. class Check_Box implements ActionListener
  5. {
  6. 	static JTextField text;
  7. 	static Checkbox checkbox;
  8. 	//Driver function
  9. 	public static void main(String args[])
  10. 	{
  11. 		//Create a frame
  12. 		JFrame frame=new JFrame("Check Box");
  13. 		frame.setSize(500,500);
  14. 		frame.setBackground(Color.white);
  15. 		frame.setLayout(null);
  16. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. 		//Create a textfield
  18. 		text=new JTextField();
  19. 		text.setBounds(0,0,500,50);
  20. 		frame.add(text);
  21. 		//Create 3 buttons
  22. 		JButton get_st=new JButton("Get State");
  23. 		JButton set_st=new JButton("Set State");
  24. 		JButton get_lb=new JButton("Get Label");
  25. 		get_st.setBounds(50,80,100,50);
  26. 		set_st.setBounds(180,80,100,50);
  27. 		get_lb.setBounds(310,80,100,50);
  28. 		frame.add(get_st);
  29. 		frame.add(set_st);
  30. 		frame.add(get_lb);
  31. 		//Create an object of class
  32. 		Check_Box obj=new Check_Box();
  33. 		//Associate ActionListener with the buttons
  34. 		get_st.addActionListener(obj);
  35. 		set_st.addActionListener(obj);
  36. 		get_lb.addActionListener(obj);
  37. 		//Create a checkbox
  38. 		checkbox=new Checkbox("Sample Checkbox");
  39. 		checkbox.setBounds(150,200,200,80);
  40. 		frame.add(checkbox);
  41. 		//Display the frame
  42. 		frame.setVisible(true);
  43. 	}
  44. 	//function to call different methods based on user's choice of button
  45.         @Override
  46. 	public void actionPerformed(ActionEvent e)
  47. 	{
  48. 		String option=e.getActionCommand();
  49. 		if(option.equals("Get State"))
  50. 			get_State();
  51. 		else if(option.equals("Set State"))
  52. 			set_State();
  53. 		else
  54. 			get_Label();
  55. 	}
  56. 	//function to get the current state of the checkbox
  57. 	public void get_State()
  58. 	{
  59. 		boolean state=checkbox.getState();
  60. 		if(state==true)
  61. 			text.setText("State of Checkbox : Checked");
  62. 		else
  63. 			text.setText("State of Checkbox : Un-Checked");
  64. 	}
  65. 	//function to change the current state of the checkbox
  66. 	public void set_State()
  67. 	{
  68.                 text.setText("State of Checkbox changed from ");
  69. 		boolean state=checkbox.getState();
  70. 		if(state==true)
  71. 		{
  72. 			checkbox.setState(false);
  73. 			text.setText(text.getText()+"Checked to Un-Checked");
  74. 		}
  75. 		else
  76. 		{
  77. 			checkbox.setState(true);
  78. 			text.setText(text.getText()+"Un-Checked to Checked");
  79. 		}
  80. 	}
  81. 	//function to get the label of the checkbox
  82. 	public void get_Label()
  83. 	{
  84. 		text.setText("Label of the checkbox is : ");
  85.                 text.setText(text.getText()+checkbox.getLabel());	
  86. 	}
  87. }
Program Explanation

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.

Runtime Test Cases

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”.
java-program-checkbox-status-unchecked

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”.
java-program-checkbox-status-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”.
java-program-checkbox-unchecked-changed-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”
java-program-checkbox-checked-changed-unchecked

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”.
java-program-checkbox-label

Sanfoundry Global Education & Learning Series – Java Programs.

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