In this article Checkbox attributes with example we give the information about Checkbox is used to ask the user to select a binary option (like yes/no). It comes in the form of a category that can be checked or unchecked.
Checkbox is a useful GUI (Graphical User Interface) component in AWT (Abstract Window Toolkit), which is used to create selectable options (checkboxes) in an interface. This is a button which we can check or uncheck. It is used when we have to choose from one or more options.
Checkbox attributes with example:
AWT Checkbox Component:
It is used to ask the user to select a binary option (like yes/no). It comes in the form of a category that can be checked or unchecked. When a user clicks a checkbox, an ItemEvent is generated that can be handled.
Checkbox has many attributes in AWT (Abstract Window Toolkit) that we can use to control its appearance and functionality.
- Label
Description: The text that appears next to a checkbox is called a label.
Methods:
getLabel(): Gets the current label of the Checkbox.
setLabel(String label): Sets the label of the Checkbox.
Example:
Checkbox cb = new Checkbox(“Java”);
cb.setLabel(“Python”); // changing label
- State
Description: This property tells whether the Checkbox is checked (selected) or unchecked (deselected). When a Checkbox is checked, its state is true, and when unchecked, it is false.
Methods:
getState(): Gets the current state of the Checkbox.
setState(boolean state): Checks or unchecks the state of the checkbox.
Example:
cb.setState(true); //checking the checkbox
- CheckboxGroup
Description: Using CheckboxGroup multiple checkboxes can be grouped together, making them behave like radio buttons. Only one Checkbox can be selected in a group at a time.
Methods:
getCheckboxGroup(): Gets the current group of Checkboxes.
setCheckboxGroup(CheckboxGroup group): Sets the Checkbox to a group.
Example:
CheckboxGroup group = new CheckboxGroup();
Checkbox cb1 = new Checkbox(“Male”, group, false);
Checkbox cb2 = new Checkbox(“Female”, group, true);
4.Enabled/Disabled
Description: This property determines whether the Checkbox is enabled or disabled. If the Checkbox is inactive, it cannot be clicked.
Methods:
setEnabled(boolean status): Enables or disables the Checkbox.
isEnabled(): Checks whether the Checkbox is active or not.
Example:
cb.setEnabled(false); //disabling the checkbox
- bounds
Description: Sets the position and size of the checkbox. These limits determine where it will be displayed on the screen and what its size will be.
Methods:
setBounds(int x, int y, int width, int height): Sets the position (x, y) and size (width, height) of the Checkbox.
Example:
cb.setBounds(100, 100, 150, 50); // Setting the position and size of the checkbox
- Foreground Color
Description: Sets the text color of the checkbox.
Methods:
setForeground(Color c): Sets the text color of the Checkbox.
Example:
cb.setForeground(Color.RED); //Setting the text color of the checkbox to red
- Background Color
Description: Sets the background color of the checkbox.
Methods:
setBackground(Color c): Sets the background color of the Checkbox.
Example:
cb.setBackground(Color.YELLOW); // Setting the background color of the checkbox to yellow
8.Font
Description: Sets the font (typeface, size, and style) of the checkbox’s text.
Methods:
setFont(Font f): Sets the font of the checkbox’s text.
Example:
Font font = new Font(“Arial”, Font.BOLD, 14);
cb.setFont(font); // setting bold font to size 14
- Visible
Description: This property determines whether the Checkbox will be visible on the screen or not.
Methods:
setVisible(boolean visible): Makes the checkbox visible or invisible.
Example:
cb.setVisible(false); // make checkbox invisible
- Item Listener
Description: When a Checkbox is clicked an event is generated that can be handled via an ItemListener.
Methods:
addItemListener(ItemListener l): Adds an item listener to the checkbox, which reacts when it is checked or unchecked.
Example:
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println(“Checkbox checked or unchecked”);
}
});
//Checkbox Example in AWT
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System