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.

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

import java.awt.*;
import java.awt.event.*;
public class CheckboxExample
{
public static void main(String[] args)
{
        // Creating a Frame
        final Frame f = new Frame(“AWT Checkbox Example”);
// Creating a Checkbox
        final Checkbox cb1 = new Checkbox(“Java”);
        cb1.setBounds(100, 100,  100, 50);
        final Checkbox cb2 = new Checkbox(“Python”);
        cb2.setBounds(100, 150,  100, 50);
cb1.setForeground(Color.RED);
        cb2.setForeground(Color.RED);
cb1.setBackground(Color.YELLOW);
cb2.setBackground(Color.YELLOW);
Font font1 = new Font(“Arial”, Font.BOLD, 14);
cb1.setFont(font1);  // setting bold font to size 14
Font font2 = new Font(“Arial”, Font.BOLD, 14);
cb2.setFont(font2);  // setting bold font to size 14
// on Checkbox join ItemListener
        cb1.addItemListener(new ItemListener()
{
            public void itemStateChanged(ItemEvent e)
{
                System.out.println(“Java Checkbox: “+(cb1.getState()?”Checked”:”Unchecked”));
            }
        });
        cb2.addItemListener(new ItemListener()
{
            public void itemStateChanged(ItemEvent e)
{
                System.out.println(“Python Checkbox:”+(cb2.getState()?”Checked”:”Unchecked”));
            }
        });
//  add Checkboxes to Frame
f.add(cb1);
        f.add(cb2);
// setting in Frame
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
// Join event to close Window
        f.addWindowListener(new WindowAdapter()
{
            public void windowClosing(WindowEvent e)
{
                f.dispose();
            }        });
    }  }

Leave a Reply

Your email address will not be published. Required fields are marked *