Updated March 17, 2023
Introduction to JCheckBox in Java
The JCheckBox class in JAVA can be used as a toggle to switch off or on any functionality. This class basically created a checkbox that provides two options which are: on and off. Here on and off are denoted as true or false internally by the system. Then on “on” state is arrived at by clicking on the checkbox. Clicking on it again changes the state of the checkbox from “on” to “off”. This class inherits its characteristics from the JToggleButton class. There are multiple variations that can be applied to this class, which we will discuss in this article further.
Syntax and Program to Implement JCheckBox in Java
The JCheckBox class can be declared as below:
Code:
public class JCheckBox extends JToggleButton implements Accessible
Explanation: Here, public” is an access modifier which states that any external or internal function can use this class. “JToggleButton” is the parent class whose characteristics/JCheckBox is using properties.
Code:
// This is the java Program created to explain the JCheckBox class in JAVA. We should get checkboxes in the output of this program.
import java.awt.event.*; // these are the main libraries imported to inherit important classes and functions from JAVA standard event library
import java.awt.*;
import javax.swing.*; // Importing swing library
class test extends JFrame {
// declaring new frame
static JFrame tf;
//Here main class will start
public static void main(String[] args)
{
// JFrame function is used to cerate a new frame.
tf = new JFrame("frame");
// This function will help in decidong the layout of the frame.
tf.setLayout(new FlowLayout());
// We are creating two checkboxes here by invoking the object of JCheckBox class.
JCheckBox cbox1 = new JCheckBox("JCheckBox 1", true);
JCheckBox cbox2 = new JCheckBox("JCheckBox 2");
// JPanel is a class which we will use to create an object of. This object is then invoked to add check bx on this panel created.
JPanel pl = new JPanel();
// This function is adding the above defined check boxes to the panel.
pl.add(cbox1);
pl.add(cbox2);
// This function will add a panel to frame.
tf.add(pl);
// This function will set the frame size
tf.setSize(400, 400);
tf.show(); // This function will help in showing the frame defined above.
}
}
Output:
The constructor of JCheckBox in Java
There are several constructors that can be used to invoke this class. Some of them are explained below:
- JJCheckBox(): This constructor creates a blank checkbox containing no text or icon. Checkbox created using this constructor is by default unselected.
- JChechBox(String s): This constructor will lead to creating an unselected checkbox, but this checkbox will contain a text passed in string data type format via a constructor parameter. In this case, we have passed String “S”, which will be displayed with the checkbox.
- JCheckBox(String text, boolean selected): This constructor is used if the requirement is to have a checkbox that is selected by default. This functionality was enabled via a boolean value “on,” which is sent via a parameter to this constructor. The text is also displayed with this constructor’s help, which is passed via parameters as the previous constructor.
- JCheckBox(Action a): This constructor creates a checkbox having properties derived from the action. The action is supplied with all the user required properties to the checkbox constructor. The checkbox then derives its properties from the action.
- JCheckBox(Icon i): This constructor returns a checkbox containing an Icon that is passed to the checkbox via a parameter in the form of “Icon i”.
- JCheckBox(Icon I, boolean selected): This constructor returns a checkbox containing an Icon along with an “on” state, which is passed to the checkbox via parameters. This checkbox will be selected by default.
- JCheckBox(String text, Icon I, boolean selected): This constructor will return a checkbox having all the three properties applied to the checkbox. These three properties are text, icon, and state, which are passed via constructor parameters.
Methods of JCheckBox in Java
The below explained are methods that are linked with JCheckBox classes in Java:
- AccessibleContext getAccessibleContext(): This method is used to get Accessible Context which is linked with this checkbox. AccessibleContext class is responsible for the information related to all accessible objects. This information contains accessible role, name and the state of an object and other related information. So, this class provides more accessibility to the users and make the interface user-friendly.
- protected String paramString(): This method is used to get a string representing the state of JCheckBox. This method is generally used by debuggers while debugging. The string returned may vary as per its implementation; it may be null as well.
- getStateChange(): This method returns true if the value of the checkbox is changed. For example, the value of the checkbox has been changed from the state “on” to “off” or vice versa; then this transition is recorded via this method. This function is used in case we want to trigger any action based on the change in the value of the checkbox. This is linked to an item listener of the checkbox.
- setSelected(boolean b): This method is used to set the checkbox with the state “on” or “off” bases on the parameter value passed. The Boolean true means “on” and false means “off”.
- getText(): This function is used to get the text of the checkbox. We capture that returned text and can use it as per the requirement of the user.
- setText(String s): Similar to the previous method, this method is used to set the text to the checkbox. The text which is passed as a parameter in this method is passed a stext in the checkbox.
Conclusion
Hence, JCheckBox class is designed in java swings to implement JtoggleButton class functions to have the resultant checkbox. The checkbox can default as selected or unselected based on the constructor we use to invoke it. The checkboxes form an important element of the forms to get the value that the user is not expected to type. For example, we want to record what all certifications have been done by the applicant then, in that case, we can give checkboxes with text containing the certification name. For these options, the user can select one or multiple checkboxes. The response can be recorded for further reference thereafter. This is an important tool for data collection used in almost all application forms. Its application can also be seen in MCQs.
Recommended Articles
This is a guide to JCheckBox in Java. Here we discuss implementing JCheckBox in Java, with constructors and methods. You can also go through our other related articles to learn more-