javax.swing

Class AbstractButton

Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, SwingConstants
Known Direct Subclasses:
JButton, JMenuItem, JToggleButton

public abstract class AbstractButton
extends JComponent
implements ItemSelectable, SwingConstants

Provides an abstract implementation of common button behaviour, data model and look & feel.

This class is supposed to serve as a base class for several kinds of buttons with similar but non-identical semantics: toggle buttons (radio buttons and checkboxes), simple push buttons, menu items, etc.

Buttons have many properties, some of which are stored in this class while others are delegated to the button's model. The following properties are available:

Property Stored inBound?
action buttonno
actionCommand modelno
borderPainted buttonyes
contentAreaFilled buttonyes
disabledIcon buttonyes
disabledSelectedIcon buttonyes
displayedMnemonicIndex buttonno
enabled modelno
focusPainted buttonyes
horizontalAlignment buttonyes
horizontalTextPosition buttonyes
icon buttonyes
iconTextGap buttonno
label (same as text) modelyes
margin buttonyes
multiClickThreshold buttonno
pressedIcon buttonyes
rolloverEnabled buttonyes
rolloverIcon buttonyes
rolloverSelectedIcon buttonyes
selected modelno
selectedIcon buttonyes
selectedObjects buttonno
text modelyes
UI buttonyes
verticalAlignment buttonyes
verticalTextPosition buttonyes

The various behavioral aspects of these properties follows:

See Also:
Serialized Form

Nested Class Summary

protected abstract class
AbstractButton.AccessibleAbstractButton
A Java Accessibility extension of the AbstractButton.
protected class
AbstractButton.ButtonChangeListener
An extension of ChangeListener to be serializable.

Nested classes/interfaces inherited from class javax.swing.JComponent

JComponent.AccessibleJComponent

Nested classes/interfaces inherited from class java.awt.Container

Container.AccessibleAWTContainer

Nested classes/interfaces inherited from class java.awt.Component

Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy

Field Summary

static String
BORDER_PAINTED_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "borderPainted" property changes.
static String
CONTENT_AREA_FILLED_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "contentAreaFilled" property changes.
static String
DISABLED_ICON_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "disabledIcon" property changes.
static String
DISABLED_SELECTED_ICON_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "disabledSelectedIcon" property changes.
static String
FOCUS_PAINTED_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "focusPainted" property changes.
static String
HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "horizontalAlignment" property changes.
static String
HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "horizontalTextPosition" property changes.
static String
ICON_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "icon" property changes.
static String
MARGIN_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "margin" property changes.
static String
MNEMONIC_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "mnemonic" property changes.
static String
MODEL_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "model" property changes.
static String
PRESSED_ICON_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "pressedIcon" property changes.
static String
ROLLOVER_ENABLED_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "rolloverEnabled" property changes.
static String
ROLLOVER_ICON_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "rolloverIcon" property changes.
static String
ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "rolloverSelectedIcon" property changes.
static String
SELECTED_ICON_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "selectedIcon" property changes.
static String
TEXT_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "text" property changes.
static String
VERTICAL_ALIGNMENT_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "verticalAlignment" property changes.
static String
VERTICAL_TEXT_POSITION_CHANGED_PROPERTY
Fired in a PropertyChangeEvent when the "verticalTextPosition" property changes.
protected ActionListener
actionListener
Listener the button uses to receive ActionEvents from its model.
protected ChangeEvent
changeEvent
ChangeEvent that is fired to button's ChangeEventListeners
protected ChangeListener
changeListener
Listener the button uses to receive ChangeEvents from its model.
protected ItemListener
itemListener
Listener the button uses to receive ItemEvents from its model.
protected ButtonModel
model
The button's current state.

Fields inherited from class javax.swing.JComponent

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

Fields inherited from class java.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT

Fields inherited from interface java.awt.image.ImageObserver

ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

Fields inherited from interface javax.swing.SwingConstants

BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST

Constructor Summary

AbstractButton()
Creates a new AbstractButton object.

Method Summary

void
addActionListener(ActionListener l)
Adds an ActionListener to the button's listener list.
void
addChangeListener(ChangeListener l)
Adds a ChangeListener to the button's listener list.
protected void
addImpl(Component comp, Object constraints, int index)
Adds the specified component to this AbstractButton.
void
addItemListener(ItemListener l)
Adds an ItemListener to the button's listener list.
protected int
checkHorizontalKey(int key, String exception)
Verifies that a particular key is one of the valid constants used for describing horizontal alignment and positioning.
protected int
checkVerticalKey(int key, String exception)
Verifies that a particular key is one of the valid constants used for describing vertical alignment and positioning.
protected void
configurePropertiesFromAction(Action a)
Configure various properties of the button by reading properties of an Action.
protected ActionListener
createActionListener()
A factory method which should return an ActionListener that propagates events from the button's ButtonModel to any of the button's ActionListeners.
protected PropertyChangeListener
createActionPropertyChangeListener(Action a)
A factory method which should return a PropertyChangeListener that accepts changes to the specified Action and reconfigure the AbstractButton, by default using the configurePropertiesFromAction(Action) method.

The button calls this method whenever a new Action is assigned to the button's "action" property, via setAction(Action), and stores the resulting PropertyChangeListener in its actionPropertyChangeListener member field.

protected ChangeListener
createChangeListener()
Factory method which creates a ChangeListener, used to subscribe to ChangeEvents from the button's model.
protected ItemListener
createItemListener()
Factory method which creates a ItemListener, used to subscribe to ItemEvents from the button's model.
void
doClick()
Programmatically perform a "click" on the button: arming, pressing, waiting, un-pressing, and disarming the model.
void
doClick(int pressTime)
Programmatically perform a "click" on the button: arming, pressing, waiting, un-pressing, and disarming the model.
protected void
fireActionPerformed(ActionEvent e)
Calls ActionListener.actionPerformed(ActionEvent) on each ActionListener in the button's listener list.
protected void
fireItemStateChanged(ItemEvent e)
Calls ItemListener.itemStateChanged(ItemEvent) on each ItemListener in the button's listener list.
protected void
fireStateChanged()
Calls ChangeListener.stateChanged(ChangeEvent) on each ChangeListener in the button's listener list.
Action
getAction()
Get the value of the "action" property.
String
getActionCommand()
Returns the action command string for this button's model.

If the action command was set to null, the button's text (label) is returned instead.

ActionListener[]
getActionListeners()
Returns all added ActionListener objects.
ChangeListener[]
getChangeListeners()
Returns all added ChangeListener objects.
Icon
getDisabledIcon()
Return the button's "disabledIcon" property.
Icon
getDisabledSelectedIcon()
Return the button's disabled selected icon.
int
getDisplayedMnemonicIndex()
Get the button's mnemonic index, which is an offset into the button's "text" property.
int
getHorizontalAlignment()
Set the horizontal alignment of the button's text and icon.
int
getHorizontalTextPosition()
Get the horizontal position of the button's text relative to its icon.
Icon
getIcon()
Return the button's default "icon" property.
int
getIconTextGap()
Get the value of the iconTextGap property.
ItemListener[]
getItemListeners()
Returns all added ItemListener objects.
String
getLabel()
Deprecated. use getText()
Insets
getMargin()
Return the button's "margin" property, which is an Insets object describing the distance between the button's border and its text and icon.
int
getMnemonic()
Get the current keyboard mnemonic value.
ButtonModel
getModel()
Get the model the button is currently using.
long
getMultiClickThreshhold()
Returns the current time in milliseconds in which clicks gets coalesced into a single ActionEvent.
Icon
getPressedIcon()
Return the button's "pressedIcon" property.
Icon
getRolloverIcon()
Return the button's rollover icon.
Icon
getRolloverSelectedIcon()
Return the button's rollover selected icon.
Icon
getSelectedIcon()
Return the button's selected icon.
Object[]
getSelectedObjects()
Returns an single-element array containing the "text" property of the button if the "selected" property of the button's model is true, otherwise returns null.
String
getText()
Return the button's "text" property.
ButtonUI
getUI()
Set the "UI" property of the button, which is a look and feel class responsible for handling the button's input events and painting it.
int
getVerticalAlignment()
Get the vertical alignment of the button's text and icon.
int
getVerticalTextPosition()
Get the vertical position of the button's text relative to its icon.
boolean
imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
Called when image data becomes available for one of the button's icons.
protected void
init(String text, Icon icon)
boolean
isBorderPainted()
Set the value of the "borderPainted" property.
boolean
isContentAreaFilled()
Returns the value of the button's "contentAreaFilled" property.
boolean
isFocusPainted()
Return the button's "paintFocus" property.
boolean
isRolloverEnabled()
Returns whether or not rollover icon changes are enabled on the button.
boolean
isSelected()
Get the value of the button's "selected" property.
protected void
paintBorder(Graphics g)
Paints the button's border, if the button's "borderPainted" property is true, by out calling to the button's look and feel class.
protected String
paramString()
Returns a string, used only for debugging, which identifies or somehow represents this button.
void
removeActionListener(ActionListener l)
Removes an ActionListener from the button's listener list.
void
removeChangeListener(ChangeListener l)
Removes a ChangeListener from the button's listener list.
void
removeItemListener(ItemListener l)
Removes an ItemListener from the button's listener list.
void
setAction(Action a)
Set the button's "action" property, subscribing the new action to the button, as an ActionListener, if it is not already subscribed.
void
setActionCommand(String actionCommand)
Sets the action command string for this button's model.
void
setBorderPainted(boolean b)
Set the value of the "borderPainted" property.
void
setContentAreaFilled(boolean b)
Sets the value of the button's "contentAreaFilled" property.
void
setDisabledIcon(Icon d)
Set the button's "disabledIcon" property.
void
setDisabledSelectedIcon(Icon icon)
Set the button's disabled selected icon.
void
setDisplayedMnemonicIndex(int index)
Sets the button's mnemonic index.
void
setEnabled(boolean b)
Enables or disables the button.
void
setFocusPainted(boolean p)
Set the button's "paintFocus" property.
void
setHorizontalAlignment(int a)
Set the horizontal alignment of the button's text and icon.
void
setHorizontalTextPosition(int t)
Set the horizontal position of the button's text relative to its icon.
void
setIcon(Icon i)
Set the button's default "icon" property.
void
setIconTextGap(int i)
Set the value of the iconTextGap property.
void
setLabel(String label)
Deprecated. use setText(text)
void
setLayout(LayoutManager layout)
Sets a layout manager on this AbstractButton.
void
setMargin(Insets m)
Set the button's "margin" property, which is an Insets object describing the distance between the button's border and its text and icon.
void
setMnemonic(char mne)
Set the current keyboard mnemonic value.
void
setMnemonic(int mne)
Set the current keyboard mnemonic value.
void
setModel(ButtonModel newModel)
Set the model the button is currently using.
void
setMultiClickThreshhold(long threshhold)
Sets the time in milliseconds in which clicks gets coalesced into a single ActionEvent.
void
setPressedIcon(Icon pressedIcon)
Set the button's "pressedIcon" property.
void
setRolloverEnabled(boolean r)
Set the "rolloverEnabled" property.
void
setRolloverIcon(Icon r)
Set the button's rollover icon and sets the rolloverEnabled property to true.
void
setRolloverSelectedIcon(Icon r)
Set the button's rollover selected icon and sets the rolloverEnabled property to true.
void
setSelected(boolean s)
Set the value of the button's "selected" property.
void
setSelectedIcon(Icon s)
Set the button's selected icon.
void
setText(String t)
Set the button's "text" property.
void
setUI(ButtonUI ui)
Set the "UI" property of the button, which is a look and feel class responsible for handling the button's input events and painting it.
void
setVerticalAlignment(int a)
Set the vertical alignment of the button's text and icon.
void
setVerticalTextPosition(int t)
Set the vertical position of the button's text relative to its icon.
void
updateUI()
Set the "UI" property to a class constructed, via the UIManager, from the current look and feel.

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait,