An implementation of a "push" button. See How to Use Buttons, Check Boxes, and Radio Buttons in The Java Tutorial for information and examples of using buttons.
For the keyboard keys used by this component in the standard Look and
Feel (L&F) renditions, see the
JButton key assignments.
| Field Summary |
| 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 button with no set text or icon. |
|
JButton(Action a)
Creates a button where properties are taken from the Action supplied. |
|
JButton(Icon icon)
Creates a button with an icon. |
|
JButton(String text)
Creates a button with text. |
|
JButton(String text,
Icon icon)
Creates a button with initial text and an icon. |
|
| Method Summary | |
void |
addActionListener(ActionListener l)
Adds an ActionListener to the button. |
void |
doClick()
Programmatically perform a "click". |
void |
doClick(int pressTime)
Programmatically perform a "click". |
Action |
getAction()
Returns the currently set Action for this
ActionEvent source, or null
if no Action is set. |
String |
getActionCommand()
Returns the action command for this button. |
ActionListener[] |
getActionListeners()
Returns an array of all the ActionListeners added
to this AbstractButton with addActionListener(). |
Icon |
getDisabledIcon()
Returns the icon used by the button when it's disabled. |
Icon |
getDisabledSelectedIcon()
Returns the icon used by the button when it's disabled and selected. |
int |
getDisplayedMnemonicIndex()
Returns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character. |
int |
getHorizontalAlignment()
Returns the horizontal alignment of the icon and text. |
int |
getHorizontalTextPosition()
Returns the horizontal position of the text relative to the icon. |
Icon |
getIcon()
Returns the default icon. |
int |
getIconTextGap()
Returns the amount of space between the text and the icon displayed in this button. |
ItemListener[] |
getItemListeners()
Returns an array of all the ItemListeners added
to this AbstractButton with addItemListener(). |
String |
getLabel()
Deprecated. - Replaced by getText |
Insets |
getMargin()
Returns the margin between the button's border and the label. |
int |
getMnemonic()
Returns the keyboard mnemonic from the the current model. |
ButtonModel |
getModel()
Returns the model that this button represents. |
long |
getMultiClickThreshhold()
Gets the amount of time (in milliseconds) required between mouse press events for the button to generate the corresponding action events. |
Icon |
getPressedIcon()
Returns the pressed icon for the button. |
Icon |
getRolloverIcon()
Returns the rollover icon for the button. |
Icon |
getRolloverSelectedIcon()
Returns the rollover selection icon for the button. |
Icon |
getSelectedIcon()
Returns the selected icon for the button. |
Object[] |
getSelectedObjects()
Returns an array (length 1) containing the label or null if the button is not selected. |
String |
getText()
Returns the button's text. |
ButtonUI |
getUI()
Returns the L&F object that renders this component. |
int |
getVerticalAlignment()
Returns the vertical alignment of the text and icon. |
int |
getVerticalTextPosition()
Returns the vertical position of the text relative to the icon. |
boolean |
imageUpdate(Image img,
int infoflags,
int x,
int y,
int w,
int h)
This is overridden to return false if the current Icon's
Image is not equal to the
passed in Image img. |
protected void |
init(String text,
Icon icon)
|
boolean |
isBorderPainted()
Gets the borderPainted property. |
boolean |
isContentAreaFilled()
Gets the contentAreaFilled property. |
boolean |
isFocusPainted()
Gets the paintFocus property. |
boolean |
isRolloverEnabled()
Gets the rolloverEnabled property. |
boolean |
isSelected()
Returns the state of the button. |
protected void |
paintBorder(Graphics g)
Paint the button's border if BorderPainted
property is true and the button has a border. |
protected String |
paramString()
Returns a string representation of this AbstractButton. |
void |
removeActionListener(ActionListener l)
Removes an ActionListener from the button. |
void |
removeChangeListener(ChangeListener l)
Removes a ChangeListener from the button. |
void |
removeItemListener(ItemListener l)
Removes an ItemListener from the button. |
void |
setAction(Action a)
Sets the Action for the ActionEvent source. |
void |
setActionCommand(String actionCommand)
Sets the action command for this button. |
void |
setBorderPainted(boolean b)
Sets the borderPainted property. |
void |
setContentAreaFilled(boolean b)
Sets the contentAreaFilled property. |
void |
setDisabledIcon(Icon disabledIcon)
Sets the disabled icon for the button. |
void |
setDisabledSelectedIcon(Icon disabledSelectedIcon)
Sets the disabled selection icon for the button. |
void |
setDisplayedMnemonicIndex(int index)
Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic. |
void |
setEnabled(boolean b)
Enables (or disables) the button. |
void |
setFocusPainted(boolean b)
Sets the paintFocus property, which must
be true for the focus state to be painted. |
void |
setHorizontalAlignment(int alignment)
Sets the horizontal alignment of the icon and text. |
void |
setHorizontalTextPosition(int textPosition)
Sets the horizontal position of the text relative to the icon. |
void |
setIcon(Icon defaultIcon)
Sets the button's default icon. |
void |
setIconTextGap(int iconTextGap)
If both the icon and text properties are set, this property defines the space between them. |
void |
setLabel(String label)
Deprecated. - Replaced by setText(text) |
void |
setMargin(Insets m)
Sets space for margin between the button's border and the label. |
void |
setMnemonic(char mnemonic)
This method is now obsolete, please use setMnemonic(int)
to set the mnemonic for a button. |
void |
setMnemonic(int mnemonic)
Sets the keyboard mnemonic on the current model. |
void |
setModel(ButtonModel newModel)
Sets the model that this button represents. |
void |
setMultiClickThreshhold(long threshhold)
Sets the amount of time (in milliseconds) required between mouse press events for the button to generate the corresponding action events. |
void |
setPressedIcon(Icon pressedIcon)
Sets the pressed icon for the button. |
void |
setRolloverEnabled(boolean b)
Sets the rolloverEnabled property, which
must be true for rollover effects to occur. |
void |
setRolloverIcon(Icon rolloverIcon)
Sets the rollover icon for the button. |
void |
setRolloverSelectedIcon(Icon rolloverSelectedIcon)
Sets the rollover selected icon for the button. |
void |
setSelected(boolean b)
Sets the state of the button. |
void |
setSelectedIcon(Icon selectedIcon)
Sets the selected icon for the button. |
void |
setText(String text)
Sets the button's text. |
void |
setUI(ButtonUI ui)
Sets the L&F object that renders this component. |
void |
setVerticalAlignment(int alignment)
Sets the vertical alignment of the icon and text. |
void |
setVerticalTextPosition(int textPosition)
Sets the vertical position of the text relative to the icon. |
void |
updateUI()
Resets the UI property to a value from the current look and feel. |
| Methods inherited from class java.lang.Object | |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
|
protected void |
configurePropertiesFromAction(Action a)
Factory method which sets the AbstractButton's properties
according to values from the Action instance. |
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this
JButton. |
String |
getUIClassID()
Returns a string that specifies the name of the L&F class that renders this component. |
boolean |
isDefaultButton()
Gets the value of the defaultButton property,
which if true means that this button is the current
default button for its JRootPane. |
boolean |
isDefaultCapable()
Gets the value of the defaultCapable property. |
protected String |
paramString()
Returns a string representation 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 determines whether this button can be
made the default button for its root pane. |
void |
updateUI()
Resets the UI property to a value from the current look and feel. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public JButton()
public JButton(Icon icon)
icon - the Icon image to display on the buttonpublic JButton(String text)
text - the text of the buttonpublic JButton(Action a)
Action supplied.
a - the Action used to specify the new button
public JButton(String text,
Icon icon)
text - the text of the buttonicon - the Icon image to display on the button| Method Detail |
public void updateUI()
updateUI in class AbstractButtonJComponent.updateUI()public String getUIClassID()
getUIClassID in class JComponentJComponent.getUIClassID(),
UIDefaults.getUI(javax.swing.JComponent)public boolean isDefaultButton()
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.
defaultButton propertyJRootPane.setDefaultButton(javax.swing.JButton),
isDefaultCapable()public boolean isDefaultCapable()
defaultCapable property.
defaultCapable propertysetDefaultCapable(boolean),
isDefaultButton(),
JRootPane.setDefaultButton(javax.swing.JButton)public void setDefaultCapable(boolean defaultCapable)
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.
defaultCapable - true if this button will be
capable of being the default button on the
RootPane; otherwise falseisDefaultCapable()public void removeNotify()
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.
removeNotify in class JComponentJComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)protected void configurePropertiesFromAction(Action a)
AbstractButton's properties
according to values from the Action instance.
The properties which get set may differ for AbstractButton
subclasses. By default, the properties which get set are
Text, Icon, Enabled, ToolTipText, ActionCommand, and
Mnemonic.
configurePropertiesFromAction in class AbstractButtona - the Action from which to get the
properties, or nullAction,
AbstractButton.setAction(javax.swing.Action)protected String paramString()
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.
paramString in class AbstractButtonJButtonpublic AccessibleContext getAccessibleContext()
AccessibleContext associated with this
JButton. For JButtons,
the AccessibleContext takes the form of an
AccessibleJButton.
A new AccessibleJButton instance is created if necessary.
getAccessibleContext in interface AccessiblegetAccessibleContext in class JComponentAccessibleJButton that serves as the
AccessibleContext of this JButton
|
JavaTM 2 Platform Std. Ed. v1.4.2 |
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
Copyright 2003 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.