JCheckBox Class

Package: javax.swing

The JCheckBox class creates a check box that the user can click (select) to check or clear. Check boxes usually let the user specify a Yes or No setting for an option. Figure 5-3 shows a frame with three check boxes.

9781118239742-fg0503.tif

Figure 5-3

Constructors

Constructor

Description

JCheckBox()

Creates a new check box that is initially unchecked.

JCheckBox(String text)

Creates a new check box that displays the specified text.

JCheckBox(String text, boolean selected)

Creates a new check box with the specified text. The boolean parameter determines whether the check box is initially checked (true) or unchecked (false).

Methods

Method

Description

void addActionListener(ActionListener listener)

Adds an ActionListener to listen for action events

void addItemListener (ItemListener listener)

Adds an ItemListener to listen for item events

String getText()

Gets the text displayed by the check box

Boolean isSelected()

Returns true if the check box is checked or false if the check box is not checked

void setSelected (boolean value)

Checks the check box if the parameter is true; unchecks it if the parameter is false

void setText(String text)

Sets the check box text

void setToolTipText(String text)

Sets the tooltip text that’s displayed if the user rests the mouse over the check box for ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.