Home Button
Contents
Swing Logo

Swinging Duke
Feedback Button
Left ArrowRight Arrow

The Swing Button API

In Swing, as in other implementations of UI components, a button is a simple atom component that can be equipped with a text label and an image, both of which are optional. When a button is pressed and released, an ActionEvent is sent to all registered ActionListeners. Swing supports several extra features of buttons, including the following:


Swing's Button Class Hierarchy

In this first release of Swing, JButton, JCheckbox, and JRadioButton are all separate classes. The JavaSoft division of Sun Microsystems Inc. is now in the process of changing that arrangement to the following inheritance scheme, in which four different button classes -- JButton, JToggleButton, JCheckbox, and JRadioButton -- are all descended from an abstract class named AbstractButton:

Diagram: Button Hierarchy

The goals for moving to this new system are:


The Button Model Interface

Currently, the button model interface used in Swing is shared by buttons, checkboxes, toggle buttons, and radio buttons. Buttons are a very UI-centric component; they don't have much of a state, other than the current visible state of the button (such as isPressed) and a list of interested listeners. The isSelected state is used by checkboxes and radio buttons.

public interface ButtonModel
{
  boolean isPressed();
  boolean isSelected();
  boolean isArmed();
  boolean isDisabled();
  boolean isRollover();
 
  public void setPressed(boolean b);
  public void setSelected(boolean b);
  public void setArmed(boolean b);
  public void setDisabled(boolean b);
  public void setRollover(boolean b);
 
    public void setKeyAccelerator(char aKey);
    public char getKeyAccelerator();
 
    public void setActionCommand(String s);
    public String getActionCommand();
 
    public void setGroup(ButtonGroup group);
    
    void addActionListener(ActionListener l);
    void removeActionListener(ActionListener l);
 
    void addItemListener(ItemListener l);
    void removeItemListener(ItemListener l);
 
    void addChangeListener(ChangeListener l);
    void removeChangeListener(ChangeListener l);
}


Events

Buttons are a source of ActionEvents. By registering itself as an ActionListener on a button, an object can make sure that it will be notified when a button has been "clicked".


The API

The API for Buttons has changed from the 1.1 API. Deprecated convenience methods are provided for backward source compatibility.



Deprecated APIs

The following old Button APIs are deprecated in Swing:

getLabel()
setLabel()

Arrows


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

Sun's Home Page