APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public abstract Class AbstractButton

extends JComponent
implements ItemSelectable, SwingConstants
Class Inheritance
All Implemented Interfaces
javax.swing.SwingConstants, java.awt.ItemSelectable
Known Direct Subclasses
javax.swing.JButton, javax.swing.JMenuItem, javax.swing.JToggleButton
Annotations
@JavaBean
defaultProperty:UI
@SuppressWarnings:serial
Imports
java.awt.*, java.awt.event.*, java.text.*, java.awt.geom.*, java.beans.JavaBean, .BeanProperty, .PropertyChangeEvent, .PropertyChangeListener, .Transient, java.util.Enumeration, java.io.Serializable, javax.swing.event.*, javax.swing.plaf.*, javax.accessibility.*, javax.swing.text.*

Defines common behaviors for buttons and menu items.

Buttons can be configured, and to some degree controlled, by Actions. Using an Action with a button has many benefits beyond directly configuring a button. Refer to Swing Components Supporting Action for more details, and you can find more information in How to Use Actions, a section in The Java Tutorial.

For further information see How to Use Buttons, Check Boxes, and Radio Buttons, a section in The Java Tutorial.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Author
Jeff Dinkins
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected abstract class
AbstractButton.AccessibleAbstractButton

This class implements accessibility support for the AbstractButton class.

private static class
protected class
AbstractButton.ButtonChangeListener

Extends ChangeListener to be serializable.

pack-priv class

Field Summary

Modifier and TypeField and Description
private Action
protected ActionListener
actionListener

The button model's ActionListener.

private PropertyChangeListener
public static final String
BORDER_PAINTED_CHANGED_PROPERTY

Identifies a change to having the border drawn, or having it not drawn.

private boolean
protected transient ChangeEvent
changeEvent

Only one ChangeEvent is needed per button instance since the event's only state is the source property.

protected ChangeListener
changeListener

The button model's changeListener.

public static final String
CONTENT_AREA_FILLED_CHANGED_PROPERTY

Identifies a change to having the button paint the content area.

private boolean
private boolean
pack-priv boolean
private Icon
private Insets
public static final String
DISABLED_ICON_CHANGED_PROPERTY

Identifies a change to the icon used when the button has been disabled.

public static final String
DISABLED_SELECTED_ICON_CHANGED_PROPERTY

Identifies a change to the icon used when the button has been disabled and selected.

private Icon
private Icon
public static final String
FOCUS_PAINTED_CHANGED_PROPERTY

Identifies a change to having the border highlighted when focused, or not.

private AbstractButton.Handler
handler

Combined listeners: ActionListener, ChangeListener, ItemListener.

private boolean
public static final String
HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY

Identifies a change in the button's horizontal alignment.

public static final String
HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY

Identifies a change in the button's horizontal text position.

private int
private int
public static final String
ICON_CHANGED_PROPERTY

Identifies a change to the icon that represents the button.

private int
private boolean
protected ItemListener
itemListener

The button model's ItemListener.

private Insets
public static final String
MARGIN_CHANGED_PROPERTY

Identifies a change in the button's margins.

private int
public static final String
MNEMONIC_CHANGED_PROPERTY

Identifies a change to the button's mnemonic.

private int
protected ButtonModel
model

The data model that determines the button's state.

public static final String
MODEL_CHANGED_PROPERTY

Identifies a change in the button model.

private long
private boolean
private boolean
public static final String
PRESSED_ICON_CHANGED_PROPERTY

Identifies a change to the icon used when the button has been pressed.

private Icon
public static final String
ROLLOVER_ENABLED_CHANGED_PROPERTY

Identifies a change from rollover enabled to disabled or back to enabled.

public static final String
ROLLOVER_ICON_CHANGED_PROPERTY

Identifies a change to the icon used when the cursor is over the button.

public static final String
ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY

Identifies a change to the icon used when the cursor is over the button and it has been selected.

private boolean
private boolean
private Icon
private Icon
public static final String
SELECTED_ICON_CHANGED_PROPERTY

Identifies a change to the icon used when the button has been selected.

private Icon
private boolean
private String
public static final String
TEXT_CHANGED_PROPERTY

Identifies a change in the button's text.

public static final String
VERTICAL_ALIGNMENT_CHANGED_PROPERTY

Identifies a change in the button's vertical alignment.

public static final String
VERTICAL_TEXT_POSITION_CHANGED_PROPERTY

Identifies a change in the button's vertical text position.

private int
private int
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
protected
AbstractButton()

Constructor for subclasses to call.

Method Summary

Modifier and TypeMethod and Description
protected void
actionPropertyChanged(Action
the Action associated with this button
action
,
String
the name of the property that changed
propertyName
)

Updates the button's state in response to property changes in the associated action.

public void
addActionListener(ActionListener
the ActionListener to be added
l
)

Adds an ActionListener to the button.

public void
addChangeListener(ChangeListener
the listener to be added
l
)

Adds a ChangeListener to the button.

protected void
addImpl(Component
the component to be added
comp
,
Object
an object expressing layout constraints for this component
constraints
,
int
the position in the container's list at which to insert the component, where -1 means append to the end
index
)

Overrides java.awt.Container.addImpl.

Adds the specified component to this container at the specified index, refer to java.awt.Container#addImpl(Component, Object, int) for a complete description of this method.

public void
addItemListener(ItemListener
the ItemListener to be added
l
)

Implements java.awt.ItemSelectable.addItemListener.

Adds an ItemListener to the checkbox.

protected int

Returns:

the key argument
checkHorizontalKey
(int
the property value to check
key
,
String
the message to use in the IllegalArgumentException that is thrown for an invalid value
exception
)

Verify that the key argument is a legal value for the horizontalAlignment and horizontalTextPosition properties.

protected int

Returns:

the key argument
checkVerticalKey
(int
the property value to check
key
,
String
the message to use in the IllegalArgumentException that is thrown for an invalid value
exception
)

Verify that the key argument is a legal value for the vertical properties.

pack-priv void
protected void
configurePropertiesFromAction(Action
the Action from which to get the properties, or null
a
)

Sets the properties on this button to match those in the specified Action.

protected ActionListener

Returns:

the ActionListener
createActionListener
()

Returns ActionListener that is added to model.

protected PropertyChangeListener

Returns:

the PropertyChangeListener
createActionPropertyChangeListener
(Action
the button's action
a
)

Creates and returns a PropertyChangeListener that is responsible for listening for changes from the specified Action and updating the appropriate properties.

pack-priv PropertyChangeListener
protected ChangeListener

Returns:

the new ChangeListener
createChangeListener
()

Subclasses that want to handle ChangeEvents differently can override this to return another ChangeListener implementation.

protected ItemListener

Returns:

the ItemListener
createItemListener
()

Returns ItemListener that is added to model.

public void
doClick()

Programmatically perform a "click".

public void
doClick(int
the time to "hold down" the button, in milliseconds
pressTime
)

Programmatically perform a "click".

protected void
fireActionPerformed(ActionEvent
the ActionEvent object
event
)

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireItemStateChanged(ItemEvent
the ItemEvent object
event
)

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireStateChanged()

Notifies all listeners that have registered interest for notification on this event type.

public Action

Returns:

the Action for this ActionEvent source, or null
getAction
()

Returns the currently set Action for this ActionEvent source, or null if no Action is set.

public String

Returns:

the action command for this button
getActionCommand
()

Returns the action command for this button.

public ActionListener[]

Returns:

all of the ActionListeners added or an empty array if no listeners have been added
getActionListeners
()

Returns an array of all the ActionListeners added to this AbstractButton with addActionListener().

public ChangeListener[]

Returns:

all of the ChangeListeners added or an empty array if no listeners have been added
getChangeListeners
()

Returns an array of all the ChangeListeners added to this AbstractButton with addChangeListener().

public Icon

Returns:

the disabledIcon property
getDisabledIcon
()

Returns the icon used by the button when it's disabled.

public Icon

Returns:

the disabledSelectedIcon property
getDisabledSelectedIcon
()

Returns the icon used by the button when it's disabled and selected.

public int

Returns:

index representing mnemonic character
getDisplayedMnemonicIndex
()

Returns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character.

private AbstractButton.Handler
public boolean

Returns:

true if the button's text property should not reflect that of the Action; the default is false
getHideActionText
()

Returns the value of the hideActionText property, which determines whether the button displays text from the Action.

public int

Returns:

the horizontalAlignment property, one of the following values:
  • SwingConstants.RIGHT
  • SwingConstants.LEFT
  • SwingConstants.CENTER
  • SwingConstants.LEADING
  • SwingConstants.TRAILING
getHorizontalAlignment
()

Returns the horizontal alignment of the icon and text.

public int

Returns:

the horizontalTextPosition property, one of the following values:
  • SwingConstants.RIGHT
  • SwingConstants.LEFT
  • SwingConstants.CENTER
  • SwingConstants.LEADING
  • SwingConstants.TRAILING (the default)
getHorizontalTextPosition
()

Returns the horizontal position of the text relative to the icon.

public Icon

Returns:

the default Icon
getIcon
()

Returns the default icon.

public int

Returns:

an int equal to the number of pixels between the text and the icon.
getIconTextGap
()

Returns the amount of space between the text and the icon displayed in this button.

public ItemListener[]

Returns:

all of the ItemListeners added or an empty array if no listeners have been added
getItemListeners
()

Returns an array of all the ItemListeners added to this AbstractButton with addItemListener().

public String

Returns:

a String containing the label
getLabel
()
Deprecated - Replaced by getText

Returns the label text.

public Insets

Returns:

an Insets object specifying the margin between the botton's border and the label
getMargin
()

Returns the margin between the button's border and the label.

public int

Returns:

the keyboard mnemonic from the model
getMnemonic
()

Returns the keyboard mnemonic from the current model.

public ButtonModel

Returns:

the model property
getModel
()

Returns the model that this button represents.

public long

Returns:

the amount of time required between mouse press events to generate corresponding action events
getMultiClickThreshhold
()

Gets the amount of time (in milliseconds) required between mouse press events for the button to generate the corresponding action events.

public Icon

Returns:

the pressedIcon property
getPressedIcon
()

Returns the pressed icon for the button.

public Icon

Returns:

the rolloverIcon property
getRolloverIcon
()

Returns the rollover icon for the button.

public Icon

Returns:

the rolloverSelectedIcon property
getRolloverSelectedIcon
()

Returns the rollover selection icon for the button.

public Icon

Returns:

the selectedIcon property
getSelectedIcon
()

Returns the selected icon for the button.

public Object[]

Returns:

an array containing 1 Object: the text of the button, if the item is selected; otherwise null
getSelectedObjects
()

Implements java.awt.ItemSelectable.getSelectedObjects.

Returns an array (length 1) containing the label or null if the button is not selected.

public String

Returns:

the buttons text
getText
()

Returns the button's text.

public ButtonUI

Returns:

the ButtonUI object
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the L&F object that renders this component.

public int

Returns:

the verticalAlignment property, one of the following values:
  • SwingConstants.CENTER (the default)
  • SwingConstants.TOP
  • SwingConstants.BOTTOM
getVerticalAlignment
()

Returns the vertical alignment of the text and icon.

public int

Returns:

the verticalTextPosition property, one of the following values:
  • SwingConstants.CENTER (the default)
  • SwingConstants.TOP
  • SwingConstants.BOTTOM
getVerticalTextPosition
()

Returns the vertical position of the text relative to the icon.

public boolean
imageUpdate(Image
the Image to be compared
img
,
int
flags used to repaint the button when the image is updated and which determine how much is to be painted
infoflags
,
int
the x coordinate
x
,
int
the y coordinate
y
,
int
the width
w
,
int
the height
h
)

Overrides java.awt.Component.imageUpdate.

Implements java.awt.image.ImageObserver.imageUpdate.

If the button icon for the current button state is either null or not an ImageIcon with an Image equal to the passed in Image, return false; otherwise it will delegate to the super-class.

protected void
init(String
the text of the button
text
,
Icon
the Icon image to display on the button
icon
)

Initialization of the AbstractButton.

public boolean

Returns:

the value of the borderPainted property
isBorderPainted
()

Gets the borderPainted property.

public boolean

Returns:

the contentAreaFilled property
isContentAreaFilled
()

Gets the contentAreaFilled property.

public boolean

Returns:

the paintFocus property
isFocusPainted
()

Gets the paintFocus property.

private boolean
public boolean

Returns:

the value of the rolloverEnabled property
isRolloverEnabled
()

Gets the rolloverEnabled property.

public boolean

Returns:

true if the toggle button is selected, otherwise false
isSelected
()

Returns the state of the button.

pack-priv void
protected void
paintBorder(Graphics
the Graphics context in which to paint
g
)

Overrides javax.swing.JComponent.paintBorder.

Paint the button's border if BorderPainted property is true and the button has a border.

protected String

Returns:

a string representation of this AbstractButton
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this AbstractButton.

public void
removeActionListener(ActionListener
the listener to be removed
l
)

Removes an ActionListener from the button.

public void
removeChangeListener(ChangeListener
the listener to be removed
l
)

Removes a ChangeListener from the button.

public void
removeItemListener(ItemListener
the ItemListener to be removed
l
)

Implements java.awt.ItemSelectable.removeItemListener.

Removes an ItemListener from the button.

public void
removeNotify()

Overrides javax.swing.JComponent.removeNotify.

Notifies this component that it no longer has a parent component.

public void
setAction(Action
the Action for the AbstractButton, or null
a
)

Sets the Action.

public void
setActionCommand(String
the action command for this button
actionCommand
)

Sets the action command for this button.

private void
public void
setBorderPainted(boolean
if true and border property is not null, the border is painted
b
)

Sets the borderPainted property.

public void
setContentAreaFilled(boolean
if true, the content should be filled; if false the content area is not filled
b
)

Sets the contentAreaFilled property.

public void
setDisabledIcon(Icon
the icon used as the disabled image
disabledIcon
)

Sets the disabled icon for the button.

public void
setDisabledSelectedIcon(Icon
the icon used as the disabled selection image
disabledSelectedIcon
)

Sets the disabled selection icon for the button.

public void
setDisplayedMnemonicIndex(int
Index into the String to underline
index
)

Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic.

private void
setDisplayedMnemonicIndexFromAction(Action a, boolean fromPropertyChange)

public void
setEnabled(boolean
true to enable the button, otherwise false
b
)

Overrides javax.swing.JComponent.setEnabled.

Enables (or disables) the button.

public void
setFocusPainted(boolean
if true, the focus state should be painted
b
)

Sets the paintFocus property, which must be true for the focus state to be painted.

public void
setHideActionText(boolean
true if the button's text property should not reflect that of the Action; the default is false
hideActionText
)

Sets the hideActionText property, which determines whether the button displays text from the Action.

public void
setHorizontalAlignment(int
the alignment value, one of the following values:
  • SwingConstants.RIGHT
  • SwingConstants.LEFT
  • SwingConstants.CENTER
  • SwingConstants.LEADING
  • SwingConstants.TRAILING
alignment
)

Sets the horizontal alignment of the icon and text.

public void
setHorizontalTextPosition(int
one of the following values:
  • SwingConstants.RIGHT
  • SwingConstants.LEFT
  • SwingConstants.CENTER
  • SwingConstants.LEADING
  • SwingConstants.TRAILING (the default)
textPosition
)

Sets the horizontal position of the text relative to the icon.

public void
setIcon(Icon
the icon used as the default image
defaultIcon
)

Sets the button's default icon.

pack-priv void
public void
setIconTextGap(int
the space between icon and text if these properties are set.
iconTextGap
)

If both the icon and text properties are set, this property defines the space between them.

public void
setLabel(String
a String containing the text
label
)
Deprecated - Replaced by setText(text)

Sets the label text.

public void
setLayout(LayoutManager
the specified layout manager
mgr
)

Overrides java.awt.Container.setLayout.

Sets the layout manager for this container, refer to java.awt.Container#setLayout(LayoutManager) for a complete description of this method.

public void
setMargin(Insets
the space between the border and the label
m
)

Sets space for margin between the button's border and the label.

public void
setMnemonic(int
the key code which represents the mnemonic
mnemonic
)

Sets the keyboard mnemonic on the current model.

public void
setMnemonic(char
a char specifying the mnemonic value
mnemonic
)

This method is now obsolete, please use setMnemonic(int) to set the mnemonic for a button.

private void
public void
setModel(ButtonModel
the new ButtonModel
newModel
)

Sets the model that this button represents.

public void
setMultiClickThreshhold(long
the amount of time required between mouse press events to generate corresponding action events
threshold
)

Sets the amount of time (in milliseconds) required between mouse press events for the button to generate the corresponding action events.

public void
setPressedIcon(Icon
the icon used as the "pressed" image
pressedIcon
)

Sets the pressed icon for the button.

public void
setRolloverEnabled(boolean
if true, rollover effects should be painted
b
)

Sets the rolloverEnabled property, which must be true for rollover effects to occur.

public void
setRolloverIcon(Icon
the icon used as the "rollover" image
rolloverIcon
)

Sets the rollover icon for the button.

public void
setRolloverSelectedIcon(Icon
the icon used as the "selected rollover" image
rolloverSelectedIcon
)

Sets the rollover selected icon for the button.

public void
setSelected(boolean
true if the button is selected, otherwise false
b
)

Sets the state of the button.

private void
setSelectedFromAction(Action
the Action
a
)

Sets the selected state of the button from the action.

public void
setSelectedIcon(Icon
the icon used as the "selected" image
selectedIcon
)

Sets the selected icon for the button.

public void
setText(String
the string used to set the text
text
)

Sets the button's text.

private void
setTextFromAction(Action a, boolean propertyChange)

public void
setUI(ButtonUI
the ButtonUI L&F object
ui
)

Sets the L&F object that renders this component.

pack-priv void
public void
setVerticalAlignment(int
one of the following values:
  • SwingConstants.CENTER (the default)
  • SwingConstants.TOP
  • SwingConstants.BOTTOM
alignment
)

Sets the vertical alignment of the icon and text.

public void
setVerticalTextPosition(int
one of the following values:
  • SwingConstants.CENTER (the default)
  • SwingConstants.TOP
  • SwingConstants.BOTTOM
textPosition
)

Sets the vertical position of the text relative to the icon.

pack-priv boolean
shouldUpdateSelectedStateFromAction()

Button subclasses that support mirroring the selected state from the action should override this to return true.

pack-priv void
private void
updateDisplayedMnemonicIndex(String text, int mnemonic)

Update the displayedMnemonicIndex property.

private void
updateMnemonicProperties()

Brings the mnemonic property in accordance with model's mnemonic.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Resets the UI property to a value from the current look and feel.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetUIClassIDgetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate