Home Button
Contents
Swing Logo

Swinging Duke
Feedback Button
Left ArrowRight Arrow

 

The JCheckbox API

This document describes the API for Swing JCheckboxes.

Description

Swing's JCheckbox class implements a checkbox object: a simple button component that appears beside a text label and contains a box in which a user can place a check mark. When the mouse is clicked inside the check area of a checkbox, an ItemEvent message is sent to all registered ItemListeners. This message contains information that specifies whether the checkbox has been selected or deselected by the user's mouse click.

Features

The JCheckbox class offers the following support for the positioning of check marks and text:

The JCheckbox Model Interface

The JCheckbox model interface is shared by JButton, JCheckbox, and JRadioButton. The isSelected state is used by JCheckbox and RadioButton, but not by Button.

public interface ButtonModel
{
  boolean isPressed();
  boolean isSelected();
  boolean isArmed();
  boolean isDisabled();
 
  public void setPressed(boolean b);
  public void setSelected(boolean b);
  public void setArmed(boolean b);
  public void setDisabled(boolean b);
 
  void addModelChangedListener(ModelChangedListener l);
  void removeModelChangedListener(ModelChangedListener l);
}

Events

JCheckbox objects are a source of ItemEvents. By registering itself as an ItemListener on a checkbox, an object can ensure that it will be notified when a checkbox has been selected or deselected.

The API

Version 0.4. Last modified 09/04/97.
In the 1.1 AWT, checkboxes pulled double duty, serving as both checkboxes and radio buttons. In 1.1, a checkbox could belong to a group in which only one checkbox could be selected at a time. In Swing, this "exclusive choice" capability is not supported. Instead, radio-button functionality is now supported by a new class named JRadioButton -- that is, by a real radio-button class.

For API compatibility, Swing's JCheckbox objects support all the old Checkbox APIs. However, all methods that were designed to support exclusive selection with CheckboxGroups have now been deprecated, and will not perform old-style Checkbox exclusive-selection functionality.

Deprecated APIs

The following Checkbox APIs have been deprecated in Swing:

Checkbox(String label, 
	 boolean state, 
	 CheckboxGroup group)	  // will create a checkbox, but ignores the
				  // group argument

Checkbox(String label, 
	 CheckboxGroup group, 
	 boolean state)		  // will create a checkbox, but ignores the
				  // group argument

getLabel()  			  // replaced by getText()

setLabel(String label)  	  // replaced by setText(String text)

getState()  			  // replaced by isSelected()

setState()  			  // replaced by setSelected(boolean state)

getCheckboxGroup() 		  // Use RadioButtons instead

setCheckboxGroup(CheckboxGroup g) // Use RadioButtons instead

Arrows


Version 0.4. Last modified 09/04/97.
Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.

Sun's Home Page