javax.swing
Class JButton

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.AbstractButton
                  extended by javax.swing.JButton
All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, SwingConstants
Direct Known Subclasses:
BasicArrowButton, MetalComboBoxButton

public class JButton
extends AbstractButton
implements Accessible

A general purpose push button. JButtons can display a label, an Icon or both.

See Also:
Serialized Form

Nested Class Summary
protected  class JButton.AccessibleJButton
          Accessibility support for JButtons.
 
Nested classes/interfaces inherited from class javax.swing.AbstractButton
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener
 
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
 
Fields inherited from class javax.swing.AbstractButton
actionListener, BORDER_PAINTED_CHANGED_PROPERTY, changeEvent, changeListener, CONTENT_AREA_FILLED_CHANGED_PROPERTY, DISABLED_ICON_CHANGED_PROPERTY, DISABLED_SELECTED_ICON_CHANGED_PROPERTY, FOCUS_PAINTED_CHANGED_PROPERTY, HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, ICON_CHANGED_PROPERTY, itemListener, MARGIN_CHANGED_PROPERTY, MNEMONIC_CHANGED_PROPERTY, model, MODEL_CHANGED_PROPERTY, PRESSED_ICON_CHANGED_PROPERTY, ROLLOVER_ENABLED_CHANGED_PROPERTY, ROLLOVER_ICON_CHANGED_PROPERTY, ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, SELECTED_ICON_CHANGED_PROPERTY, TEXT_CHANGED_PROPERTY, VERTICAL_ALIGNMENT_CHANGED_PROPERTY, VERTICAL_TEXT_POSITION_CHANGED_PROPERTY
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
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
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JButton()
          Creates a new button with an empty string for the button text and no icon.
JButton(Action a)
          Creates a new button from the specified action.
JButton(Icon icon)
          Creates a new button with the specified icon (and an empty string for the button text).
JButton(String text)
          Creates a new button with the specified text and no icon.
JButton(String text, Icon icon)
          Creates a new button with the specified text and icon.
 
Method Summary
protected  void configurePropertiesFromAction(Action a)
          Configure various properties of the button by reading properties of an Action.
 AccessibleContext getAccessibleContext()
          Returns the object that provides accessibility features for this JButton component.
 String getUIClassID()
          Returns the suffix ("ButtonUI" in this case) used to determine the class name for a UI delegate that can provide the look and feel for a JButton.
 boolean isDefaultButton()
          Returns true if this button is the default button in its JRootPane.
 boolean isDefaultCapable()
          Returns true if this button can act as the default button.
protected  String paramString()
          Returns an implementation-dependent string describing the attributes of this JButton.
 void removeNotify()
          Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane, and if so, sets the RootPane's default button to null to ensure the RootPane doesn't hold onto an invalid button reference.
 void setDefaultCapable(boolean defaultCapable)
          Sets the defaultCapable property which indicates if this button may become the default button in its JRootPane.
 void updateUI()
          Sets this button's UI delegate to the default (obtained from the UIManager) for the current look and feel.
 
Methods inherited from class javax.swing.AbstractButton
addActionListener, addChangeListener, addImpl, addItemListener, checkHorizontalKey, checkVerticalKey, createActionListener, createActionPropertyChangeListener, createChangeListener, createItemListener, doClick, doClick, fireActionPerformed, fireItemStateChanged, fireStateChanged, getAction, getActionCommand, getActionListeners, getChangeListeners, getDisabledIcon, getDisabledSelectedIcon, getDisplayedMnemonicIndex, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getItemListeners, getLabel, getMargin, getMnemonic, getModel, getMultiClickThreshhold, getPressedIcon, getRolloverIcon, getRolloverSelectedIcon, getSelectedIcon, getSelectedObjects, getText, getUI, getVerticalAlignment, getVerticalTextPosition, imageUpdate, init, isBorderPainted, isContentAreaFilled, isFocusPainted, isRolloverEnabled, isSelected, paintBorder, removeActionListener, removeChangeListener, removeItemListener, setAction, setActionCommand, setBorderPainted, setContentAreaFilled, setDisabledIcon, setDisabledSelectedIcon, setDisplayedMnemonicIndex, setEnabled, setFocusPainted, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabel, setLayout, setMargin, setMnemonic, setMnemonic, setModel, setMultiClickThreshhold, setPressedIcon, setRolloverEnabled, setRolloverIcon, setRolloverSelectedIcon, setSelected, setSelectedIcon, setText, setUI, setVerticalAlignment, setVerticalTextPosition
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JButton

public JButton()
Creates a new button with an empty string for the button text and no icon.


JButton

public JButton(Action a)
Creates a new button from the specified action.

Parameters:
a - the action (null permitted).
See Also:
AbstractButton.setAction(Action)

JButton

public JButton(Icon icon)
Creates a new button with the specified icon (and an empty string for the button text).

Parameters:
icon - the icon (null permitted).

JButton

public JButton(String text)
Creates a new button with the specified text and no icon.

Parameters:
text - the button text (null permitted, will be substituted by an empty string).

JButton

public JButton(String text,
               Icon icon)
Creates a new button with the specified text and icon.

Parameters:
text - the button text (null permitted, will be substituted by an empty string).
icon - the icon (null permitted).
Method Detail

configurePropertiesFromAction

protected void configurePropertiesFromAction(Action a)
Description copied from class: AbstractButton
Configure various properties of the button by reading properties of an Action. The mapping of properties is as follows:
Action keyed property AbstractButton property
NAME text
SMALL_ICON icon
SHORT_DESCRIPTION toolTipText
MNEMONIC_KEY mnemonic
ACTION_COMMAND_KEY actionCommand

In addition, this method always sets the button's "enabled" property to the value of the Action's "enabled" property.

If the provided Action is null, the text, icon, and toolTipText properties of the button are set to null, and the "enabled" property is set to true; the mnemonic and actionCommand properties are unchanged.

Overrides:
configurePropertiesFromAction in class AbstractButton
Parameters:
a - An Action to configure the button from

getAccessibleContext

public AccessibleContext getAccessibleContext()
Returns the object that provides accessibility features for this JButton component.

Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class JComponent
Returns:
The accessible context (an instance of JButton.AccessibleJButton).

getUIClassID

public String getUIClassID()
Returns the suffix ("ButtonUI" in this case) used to determine the class name for a UI delegate that can provide the look and feel for a JButton.

Overrides:
getUIClassID in class JComponent
Returns:
"ButtonUI".
See Also:
JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

isDefaultButton

public boolean isDefaultButton()
Returns true if this button is the default button in its JRootPane. The default button gets automatically activated when the user presses ENTER (or whatever key this is bound to in the current Look and Feel).

Returns:
true if this button is the default button in its JRootPane
See Also:
isDefaultCapable(), setDefaultCapable(boolean), JRootPane.getDefaultButton(), JRootPane.setDefaultButton(JButton)

isDefaultCapable

public boolean isDefaultCapable()
Returns true if this button can act as the default button. This is true by default.

Returns:
true if this button can act as the default button
See Also:
setDefaultCapable(boolean), isDefaultButton(), JRootPane.getDefaultButton(), JRootPane.setDefaultButton(JButton)

paramString

protected String paramString()
Returns an implementation-dependent string describing the attributes of this JButton.

Overrides:
paramString in class AbstractButton
Returns:
A string describing the attributes of this JButton (never null).

removeNotify

public void removeNotify()
Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane, and if so, sets the RootPane's default button to null to ensure the RootPane doesn't hold onto an invalid button reference.

Overrides:
removeNotify in class JComponent
See Also:
Component.isDisplayable(), Component.addNotify()

setDefaultCapable

public void setDefaultCapable(boolean defaultCapable)
Sets the defaultCapable property which indicates if this button may become the default button in its JRootPane.

Parameters:
defaultCapable - true if this button can become the default button in its JRootPane, false otherwise
See Also:
setDefaultCapable(boolean), isDefaultButton(), JRootPane.getDefaultButton(), JRootPane.setDefaultButton(JButton)

updateUI

public void updateUI()
Sets this button's UI delegate to the default (obtained from the UIManager) for the current look and feel.

Overrides:
updateUI in class AbstractButton