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 a few moments

Here’s an example that creates a check box:

JCheckbox pepperoni;

pepperoni = new JCheckBox(“Pepperoni”);

To create a check box that is initially checked, call the constructor like this:

JCheckbox pepperoni;

pepperoni = new JCheckBox(“Pepperoni”, true);

You can test the state of a check box by using the isSelected method:

if (pepperoni.isSelected())

{

// code to execute if pepperoni is selected

}

You can set the state of a check box by calling its setSelected method:

pepperoni.setSelected(false);

The preceding line clears the check box referred to by the pepperoni variable.

TechnicalStuff.eps If you want, you can add event listeners to check boxes to respond to events generated when the user clicks those check boxes. Check boxes support both action listeners and item listeners. The difference between them is subtle:

check.png An action event is generated whenever the user clicks a check box to change its state.

check.png An item event is generated whenever the state of the check box is changed, whether as a result of being clicked by the user or because the program called the setSelected method.

Suppose that your pizza restaurant has anchovies on the menu, but you want to discourage your customers from actually ordering them. Here’s an actionPerformed method from an action listener that displays a message if the user ordered anchovies:

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == anchovies)

{

JOptionPane.showMessageDialog(anchovies,

“Yuck! Anchovies are disgusting!”,

“Yuck!”,

JOptionPane.WARNING_MESSAGE);

anchovies.setSelected(false);

}

}

tip.eps Add a listener to a check box only if you need to provide immediate feedback to the user when he or she checks or unchecks the box. In most applications, you wait until the user clicks a button to examine the state of any check boxes in the frame.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset