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

public Class JButton

extends AbstractButton
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Known Direct Subclasses
javax.swing.plaf.basic.BasicArrowButton, javax.swing.plaf.basic.BasicInternalFrameTitlePane.NoFocusButton, javax.swing.plaf.basic.BasicOptionPaneUI.ButtonFactory.ConstrainedButton, javax.swing.plaf.metal.MetalComboBoxButton, javax.swing.plaf.synth.SynthArrowButton, com.sun.java.swing.plaf.motif.MotifDesktopIconUI.IconButton, com.sun.java.swing.plaf.motif.MotifInternalFrameTitlePane.FrameButton
Annotations
@JavaBean
defaultProperty:UIClassID
description:An implementation of a "push" button.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.beans.BeanProperty, .ConstructorProperties, .JavaBean, java.io.IOException, .ObjectOutputStream, .Serial, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, javax.swing.plaf.ButtonUI

An implementation of a "push" button.

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.

See How to Use Buttons, Check Boxes, and Radio Buttons in The Java Tutorial for information and examples of using buttons.

Warning

Swing is not thread safe. For more information see Swing's Threading Policy.

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 class
JButton.AccessibleJButton

This class implements accessibility support for the JButton class.

Field Summary

Modifier and TypeField and Description
private static final String
Inherited from javax.swing.AbstractButton:
actionListenerBORDER_PAINTED_CHANGED_PROPERTYchangeEventchangeListenerCONTENT_AREA_FILLED_CHANGED_PROPERTYdefaultCapableDISABLED_ICON_CHANGED_PROPERTYDISABLED_SELECTED_ICON_CHANGED_PROPERTYFOCUS_PAINTED_CHANGED_PROPERTYHORIZONTAL_ALIGNMENT_CHANGED_PROPERTYHORIZONTAL_TEXT_POSITION_CHANGED_PROPERTYICON_CHANGED_PROPERTYitemListenerMARGIN_CHANGED_PROPERTYMNEMONIC_CHANGED_PROPERTYmodelMODEL_CHANGED_PROPERTYPRESSED_ICON_CHANGED_PROPERTYROLLOVER_ENABLED_CHANGED_PROPERTYROLLOVER_ICON_CHANGED_PROPERTYROLLOVER_SELECTED_ICON_CHANGED_PROPERTYSELECTED_ICON_CHANGED_PROPERTYTEXT_CHANGED_PROPERTYVERTICAL_ALIGNMENT_CHANGED_PROPERTYVERTICAL_TEXT_POSITION_CHANGED_PROPERTY

Constructor Summary

AccessConstructor and Description
public
JButton()

Creates a button with no set text or icon.

public
JButton(Icon
the Icon image to display on the button
icon
)

Creates a button with an icon.

public
JButton(String
the text of the button
text
)

Creates a button with text.

public
JButton(Action
the Action used to specify the new button
a
)

Creates a button where properties are taken from the Action supplied.

public
JButton(String
the text of the button
text
,
Icon
the Icon image to display on the button
icon
)

Creates a button with initial text and an icon.

Method Summary

Modifier and TypeMethod and Description
public AccessibleContext

Returns:

an AccessibleJButton that serves as the AccessibleContext of this JButton
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JButton.

public String

Returns:

the string "ButtonUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns a string that specifies the name of the L&F class that renders this component.

public boolean

Returns:

the value of the defaultButton property
isDefaultButton
()

Gets the value of the defaultButton property, which if true means that this button is the current default button for its JRootPane.

public boolean

Returns:

the value of the defaultCapable property
isDefaultCapable
()

Gets the value of the defaultCapable property.

protected String

Returns:

a string representation of this JButton
paramString
()

Overrides javax.swing.AbstractButton.paramString.

Returns a string representation of this JButton.

public void
removeNotify()

Overrides javax.swing.AbstractButton.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.

public void
setDefaultCapable(boolean
true if this button will be capable of being the default button on the RootPane; otherwise false
defaultCapable
)

Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane.

public void
updateUI()

Overrides javax.swing.AbstractButton.updateUI.

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

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Inherited from javax.swing.AbstractButton:
actionPropertyChangedaddActionListeneraddChangeListeneraddImpladdItemListenercheckHorizontalKeycheckVerticalKeyclientPropertyChangedconfigurePropertiesFromActioncreateActionListenercreateActionPropertyChangeListenercreateActionPropertyChangeListener0createChangeListenercreateItemListenerdoClickdoClickfireActionPerformedfireItemStateChangedfireStateChangedgetActiongetActionCommandgetActionListenersgetChangeListenersgetDisabledIcongetDisabledSelectedIcongetDisplayedMnemonicIndexgetHideActionTextgetHorizontalAlignmentgetHorizontalTextPositiongetIcongetIconTextGapgetItemListenersgetLabelgetMargingetMnemonicgetModelgetMultiClickThreshholdgetPressedIcongetRolloverIcongetRolloverSelectedIcongetSelectedIcongetSelectedObjectsgetTextgetUIgetVerticalAlignmentgetVerticalTextPositionimageUpdateinitisBorderPaintedisContentAreaFilledisFocusPaintedisRolloverEnabledisSelectedlargeIconChangedpaintBorderremoveActionListenerremoveChangeListenerremoveItemListenersetActionsetActionCommandsetBorderPaintedsetContentAreaFilledsetDisabledIconsetDisabledSelectedIconsetDisplayedMnemonicIndexsetEnabledsetFocusPaintedsetHideActionTextsetHorizontalAlignmentsetHorizontalTextPositionsetIconsetIconFromActionsetIconTextGapsetLabelsetLayoutsetMarginsetMnemonicsetMnemonicsetModelsetMultiClickThreshholdsetPressedIconsetRolloverEnabledsetRolloverIconsetRolloverSelectedIconsetSelectedsetSelectedIconsetTextsetUIsetUIPropertysetVerticalAlignmentsetVerticalTextPositionshouldUpdateSelectedStateFromActionsmallIconChanged

Field Detail

uiClassIDback to summary
private static final String uiClassID

Hides javax.swing.JComponent.uiClassID.

See Also
getUIClassID, readObject

Constructor Detail

JButtonback to summary
public JButton()

Creates a button with no set text or icon.

JButtonback to summary
public JButton(Icon icon)

Creates a button with an icon.

Parameters
icon:Icon

the Icon image to display on the button

JButtonback to summary
public JButton(String text)

Creates a button with text.

Parameters
text:String

the text of the button

Annotations
@ConstructorProperties:text
JButtonback to summary
public JButton(Action a)

Creates a button where properties are taken from the Action supplied.

Parameters
a:Action

the Action used to specify the new button

Since
1.3
JButtonback to summary
public JButton(String text, Icon icon)

Creates a button with initial text and an icon.

Parameters
text:String

the text of the button

icon:Icon

the Icon image to display on the button

Method Detail

getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JButton. For JButtons, the AccessibleContext takes the form of an AccessibleJButton. A new AccessibleJButton instance is created if necessary.

Returns:AccessibleContext

an AccessibleJButton that serves as the AccessibleContext of this JButton

Annotations
@BeanProperty
bound:false
expert:true
description:The AccessibleContext associated with this Button.
getUIClassIDback to summary
public String getUIClassID()

Overrides javax.swing.JComponent.getUIClassID.

Returns a string that specifies the name of the L&F class that renders this component.

Returns:String

the string "ButtonUI"

Annotations
@BeanProperty
bound:false
expert:true
description:A string that specifies the name of the L&F class.
See Also
JComponent#getUIClassID, UIDefaults#getUI
isDefaultButtonback to summary
public boolean isDefaultButton()

Gets the value of the defaultButton property, which if true means that this button is the current default button for its JRootPane. Most look and feels render the default button differently, and may potentially provide bindings to access the default button.

Returns:boolean

the value of the defaultButton property

Annotations
@BeanProperty
bound:false
description:Whether or not this button is the default button
See Also
JRootPane#setDefaultButton, isDefaultCapable
isDefaultCapableback to summary
public boolean isDefaultCapable()

Gets the value of the defaultCapable property.

Returns:boolean

the value of the defaultCapable property

See Also
setDefaultCapable, isDefaultButton, JRootPane#setDefaultButton
paramStringback to summary
protected String paramString()

Overrides javax.swing.AbstractButton.paramString.

Returns a string representation of this JButton. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Returns:String

a string representation of this JButton

removeNotifyback to summary
public void removeNotify()

Overrides javax.swing.AbstractButton.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.

setDefaultCapableback to summary
public void setDefaultCapable(boolean defaultCapable)

Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane. The default value of the defaultCapable property is true unless otherwise specified by the look and feel.

Parameters
defaultCapable:boolean

true if this button will be capable of being the default button on the RootPane; otherwise false

Annotations
@BeanProperty
visualUpdate:true
description:Whether or not this button can be the default button
See Also
isDefaultCapable
updateUIback to summary
public void updateUI()

Overrides javax.swing.AbstractButton.updateUI.

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

See Also
JComponent#updateUI
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides javax.swing.JComponent.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Annotations
@Serial
Exceptions
IOException:

Doc from java.awt.Container.writeObject.

if an I/O error occurs

javax.swing back to summary

protected Class JButton.AccessibleJButton

extends AccessibleAbstractButton
Class Inheritance
Annotations
@SuppressWarnings:serial

This class implements accessibility support for the JButton class. It provides an implementation of the Java Accessibility API appropriate to button user-interface elements.

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.

Constructor Summary

AccessConstructor and Description
protected
AccessibleJButton()

Constructs an AccessibleJButton.

Method Summary

Modifier and TypeMethod and Description
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object
getAccessibleRole
()

Overrides javax.swing.JComponent.AccessibleJComponent.getAccessibleRole.

Get the role of this object.

Inherited from javax.swing.AbstractButton.AccessibleAbstractButton:
doAccessibleActiongetAccessibleActiongetAccessibleActionCountgetAccessibleActionDescriptiongetAccessibleExtendedComponentgetAccessibleIcongetAccessibleKeyBindinggetAccessibleNamegetAccessibleRelationSetgetAccessibleStateSetgetAccessibleTextgetAccessibleValuegetAfterIndexgetAtIndexgetBeforeIndexgetCaretPositiongetCharacterAttributegetCharacterBoundsgetCharCountgetCurrentAccessibleValuegetIndexAtPointgetMaximumAccessibleValuegetMinimumAccessibleValuegetSelectedTextgetSelectionEndgetSelectionStartgetTitledBorderTextgetToolTipTextsetCurrentAccessibleValue

Constructor Detail

AccessibleJButtonback to summary
protected AccessibleJButton()

Constructs an AccessibleJButton.

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides javax.swing.JComponent.AccessibleJComponent.getAccessibleRole.

Get the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object

See Also
AccessibleRole