JavaTM 2 Platform
Std. Ed. v1.3.1

java.awt
Class Button

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Button
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable

public class Button
extends Component
implements Accessible

This class creates a labeled button. The application can cause some action to happen when the button is pushed. This image depicts three views of a "Quit" button as it appears under the Solaris operating system:

The first view shows the button as it appears normally. The second view shows the button when it has input focus. Its outline is darkened to let the user know that it is an active object. The third view shows the button when the user clicks the mouse over the button, and thus requests that an action be performed.

The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent, which is sent out when the mouse is both pressed and released over the button. If an application is interested in knowing when the button has been pressed but not released, as a separate gesture, it can specialize processMouseEvent, or it can register itself as a listener for mouse events by calling addMouseListener. Both of these methods are defined by Component, the abstract superclass of all components.

When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. The button's processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button.

If an application wants to perform some action based on a button being pressed and released, it should implement ActionListener and register the new listener to receive events from this button, by calling the button's addActionListener method. The application can make use of the button's action command as a messaging protocol.

Since:
JDK1.0
See Also:
ActionEvent, ActionListener, Component.processMouseEvent(java.awt.event.MouseEvent), Component.addMouseListener(java.awt.event.MouseListener), Serialized Form

Inner Class Summary
protected  class Button.AccessibleAWTButton
          This class implements accessibility support for the Button class.
 
Inner classes inherited from class java.awt.Component
Component.AccessibleAWTComponent
 
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
 
Constructor Summary
Button()
          Constructs a Button with no label.
Button(String label)
          Constructs a Button with the specified label.
 
Method Summary
 void addActionListener(ActionListener l)
          Adds the specified action listener to receive action events from this button.
 void addNotify()
          Creates the peer of the button.
 AccessibleContext getAccessibleContext()
          Gets the AccessibleContext associated with this Button.
 String getActionCommand()
          Returns the command name of the action event fired by this button.
 String getLabel()
          Gets the label of this button.
 EventListener[] getListeners(Class listenerType)
          Return an array of all the listeners that were added to the Button with addXXXListener(), where XXX is the name of the listenerType argument.
protected  String paramString()
          Returns the parameter string representing the state of this button.
protected  void processActionEvent(ActionEvent e)
          Processes action events occurring on this button by dispatching them to any registered ActionListener objects.
protected  void processEvent(AWTEvent e)
          Processes events on this button.
 void removeActionListener(ActionListener l)
          Removes the specified action listener so that it no longer receives action events from this button.
 void setActionCommand(String command)
          Sets the command name for the action event fired by this button.
 void setLabel(String label)
          Sets the button's label to be the specified string.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, 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, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, update, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Button

public Button()
Constructs a Button with no label.

Button

public Button(String label)
Constructs a Button with the specified label.
Parameters:
label - A string label for the button.
Method Detail

addNotify

public void addNotify()
Creates the peer of the button. The button's peer allows the application to change the look of the button without changing its functionality.
Overrides:
addNotify in class Component
See Also:
Toolkit.createButton(java.awt.Button), Component.getToolkit()

getLabel

public String getLabel()
Gets the label of this button.
Returns:
the button's label, or null if the button has no label.
See Also:
setLabel(java.lang.String)

setLabel

public void setLabel(String label)
Sets the button's label to be the specified string.
Parameters:
label - the new label, or null if the button has no label.
See Also:
getLabel()

setActionCommand

public void setActionCommand(String command)
Sets the command name for the action event fired by this button. By default this action command is set to match the label of the button.
Parameters:
command - A string used to set the button's action command. If the string is null then the action command is set to match the label of the button.
Since:
JDK1.1
See Also:
ActionEvent

getActionCommand

public String getActionCommand()
Returns the command name of the action event fired by this button. If the command name is null (default) then this method returns the label of the button.

addActionListener

public void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this button. Action events occur when a user presses or releases the mouse over this button. If l is null, no exception is thrown and no action is performed.
Parameters:
l - the action listener
Since:
JDK1.1
See Also:
ActionListener, removeActionListener(java.awt.event.ActionListener)

removeActionListener

public void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this button. Action events occur when a user presses or releases the mouse over this button. If l is null, no exception is thrown and no action is performed.
Parameters:
l - the action listener
Since:
JDK1.1
See Also:
ActionListener, addActionListener(java.awt.event.ActionListener)

getListeners

public EventListener[] getListeners(Class listenerType)
Return an array of all the listeners that were added to the Button with addXXXListener(), where XXX is the name of the listenerType argument. For example, to get all of the ActionListener(s) for the given Button b, one would write:
 ActionListener[] als = (ActionListener[])(b.getListeners(ActionListener.class))
 
If no such listener list exists, then an empty array is returned.
Overrides:
getListeners in class Component
Parameters:
listenerType - Type of listeners requested
Returns:
all of the listeners of the specified type supported by this button
Since:
1.3

processEvent

protected void processEvent(AWTEvent e)
Processes events on this button. If an event is an instance of ActionEvent, this method invokes the processActionEvent method. Otherwise, it invokes processEvent on the superclass.
Overrides:
processEvent in class Component
Parameters:
e - the event.
Since:
JDK1.1
See Also:
ActionEvent, processActionEvent(java.awt.event.ActionEvent)

processActionEvent

protected void processActionEvent(ActionEvent e)
Processes action events occurring on this button by dispatching them to any registered ActionListener objects.

This method is not called unless action events are enabled for this button. Action events are enabled when one of the following occurs:

Parameters:
e - the action event.
Since:
JDK1.1
See Also:
ActionListener, addActionListener(java.awt.event.ActionListener), Component.enableEvents(long)

paramString

protected String paramString()
Returns the parameter string representing the state of this button. This string is useful for debugging.
Overrides:
paramString in class Component
Returns:
the parameter string of this button.

getAccessibleContext

public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Button. For buttons, the AccessibleContext takes the form of an AccessibleAWTButton. A new AccessibleAWTButton instance is created if necessary.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class Component
Returns:
an AccessibleAWTButton that serves as the AccessibleContext of this Button

JavaTM 2 Platform
Std. Ed. v1.3.1

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-2001 Sun Microsystems, Inc. 901 San Antonio Road
Palo Alto, California, 94303, U.S.A. All Rights Reserved.