ActionEvent (Java Platform SE 8 ) (original) (raw)
- java.util.EventObject
- java.awt.AWTEvent
- java.awt.event.ActionEvent
- java.awt.AWTEvent
All Implemented Interfaces:
Serializable
public class ActionEvent
extends AWTEvent
A semantic event which indicates that a component-defined action occurred. This high-level event is generated by a component (such as aButton
) when the component-specific action occurs (such as being pressed). The event is passed to every ActionListener
object that registered to receive such events using the component'saddActionListener
method.
Note: To invoke an ActionEvent
on aButton
using the keyboard, use the Space bar.
The object that implements the ActionListener
interface gets this ActionEvent
when the event occurs. The listener is therefore spared the details of processing individual mouse movements and mouse clicks, and can instead process a "meaningful" (semantic) event like "button pressed".
An unspecified behavior will be caused if the id
parameter of any particular ActionEvent
instance is not in the range from ACTION_FIRST
to ACTION_LAST
.
Since:
1.1
See Also:
ActionListener, Tutorial: How to Write an Action Listener, Serialized Form
Field Summary
Fields
Modifier and Type Field Description static int ACTION_FIRST The first number in the range of ids used for action events. static int ACTION_LAST The last number in the range of ids used for action events. static int ACTION_PERFORMED This event id indicates that a meaningful action occurred. static int ALT_MASK The alt modifier. static int CTRL_MASK The control modifier. static int META_MASK The meta modifier. static int SHIFT_MASK The shift modifier. * ### Fields inherited from class java.awt.[AWTEvent](../../../java/awt/AWTEvent.html "class in java.awt") `[ACTION_EVENT_MASK](../../../java/awt/AWTEvent.html#ACTION%5FEVENT%5FMASK), [ADJUSTMENT_EVENT_MASK](../../../java/awt/AWTEvent.html#ADJUSTMENT%5FEVENT%5FMASK), [COMPONENT_EVENT_MASK](../../../java/awt/AWTEvent.html#COMPONENT%5FEVENT%5FMASK), [consumed](../../../java/awt/AWTEvent.html#consumed), [CONTAINER_EVENT_MASK](../../../java/awt/AWTEvent.html#CONTAINER%5FEVENT%5FMASK), [FOCUS_EVENT_MASK](../../../java/awt/AWTEvent.html#FOCUS%5FEVENT%5FMASK), [HIERARCHY_BOUNDS_EVENT_MASK](../../../java/awt/AWTEvent.html#HIERARCHY%5FBOUNDS%5FEVENT%5FMASK), [HIERARCHY_EVENT_MASK](../../../java/awt/AWTEvent.html#HIERARCHY%5FEVENT%5FMASK), [id](../../../java/awt/AWTEvent.html#id), [INPUT_METHOD_EVENT_MASK](../../../java/awt/AWTEvent.html#INPUT%5FMETHOD%5FEVENT%5FMASK), [INVOCATION_EVENT_MASK](../../../java/awt/AWTEvent.html#INVOCATION%5FEVENT%5FMASK), [ITEM_EVENT_MASK](../../../java/awt/AWTEvent.html#ITEM%5FEVENT%5FMASK), [KEY_EVENT_MASK](../../../java/awt/AWTEvent.html#KEY%5FEVENT%5FMASK), [MOUSE_EVENT_MASK](../../../java/awt/AWTEvent.html#MOUSE%5FEVENT%5FMASK), [MOUSE_MOTION_EVENT_MASK](../../../java/awt/AWTEvent.html#MOUSE%5FMOTION%5FEVENT%5FMASK), [MOUSE_WHEEL_EVENT_MASK](../../../java/awt/AWTEvent.html#MOUSE%5FWHEEL%5FEVENT%5FMASK), [PAINT_EVENT_MASK](../../../java/awt/AWTEvent.html#PAINT%5FEVENT%5FMASK), [RESERVED_ID_MAX](../../../java/awt/AWTEvent.html#RESERVED%5FID%5FMAX), [TEXT_EVENT_MASK](../../../java/awt/AWTEvent.html#TEXT%5FEVENT%5FMASK), [WINDOW_EVENT_MASK](../../../java/awt/AWTEvent.html#WINDOW%5FEVENT%5FMASK), [WINDOW_FOCUS_EVENT_MASK](../../../java/awt/AWTEvent.html#WINDOW%5FFOCUS%5FEVENT%5FMASK), [WINDOW_STATE_EVENT_MASK](../../../java/awt/AWTEvent.html#WINDOW%5FSTATE%5FEVENT%5FMASK)` * ### Fields inherited from class java.util.[EventObject](../../../java/util/EventObject.html "class in java.util") `[source](../../../java/util/EventObject.html#source)`
Constructor Summary
Constructors
Constructor Description ActionEvent(Object source, int id,String command) Constructs an ActionEvent object. ActionEvent(Object source, int id,String command, int modifiers) Constructs an ActionEvent object with modifier keys. ActionEvent(Object source, int id,String command, long when, int modifiers) Constructs an ActionEvent object with the specified modifier keys and timestamp. Method Summary
All Methods Instance Methods Concrete Methods
Modifier and Type Method Description String getActionCommand() Returns the command string associated with this action. int getModifiers() Returns the modifier keys held down during this action event. long getWhen() Returns the timestamp of when this event occurred. String paramString() Returns a parameter string identifying this action event. * ### Methods inherited from class java.awt.[AWTEvent](../../../java/awt/AWTEvent.html "class in java.awt") `[consume](../../../java/awt/AWTEvent.html#consume--), [getID](../../../java/awt/AWTEvent.html#getID--), [isConsumed](../../../java/awt/AWTEvent.html#isConsumed--), [setSource](../../../java/awt/AWTEvent.html#setSource-java.lang.Object-), [toString](../../../java/awt/AWTEvent.html#toString--)` * ### Methods inherited from class java.util.[EventObject](../../../java/util/EventObject.html "class in java.util") `[getSource](../../../java/util/EventObject.html#getSource--)` * ### Methods inherited from class java.lang.[Object](../../../java/lang/Object.html "class in java.lang") `[clone](../../../java/lang/Object.html#clone--), [equals](../../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../../java/lang/Object.html#finalize--), [getClass](../../../java/lang/Object.html#getClass--), [hashCode](../../../java/lang/Object.html#hashCode--), [notify](../../../java/lang/Object.html#notify--), [notifyAll](../../../java/lang/Object.html#notifyAll--), [wait](../../../java/lang/Object.html#wait--), [wait](../../../java/lang/Object.html#wait-long-), [wait](../../../java/lang/Object.html#wait-long-int-)`
Field Detail
* #### SHIFT\_MASK public static final int SHIFT_MASK The shift modifier. An indicator that the shift key was held down during the event. See Also: [Constant Field Values](../../../constant-values.html#java.awt.event.ActionEvent.SHIFT%5FMASK) * #### CTRL\_MASK public static final int CTRL_MASK The control modifier. An indicator that the control key was held down during the event. See Also: [Constant Field Values](../../../constant-values.html#java.awt.event.ActionEvent.CTRL%5FMASK) * #### META\_MASK public static final int META_MASK The meta modifier. An indicator that the meta key was held down during the event. See Also: [Constant Field Values](../../../constant-values.html#java.awt.event.ActionEvent.META%5FMASK) * #### ALT\_MASK public static final int ALT_MASK The alt modifier. An indicator that the alt key was held down during the event. See Also: [Constant Field Values](../../../constant-values.html#java.awt.event.ActionEvent.ALT%5FMASK) * #### ACTION\_FIRST public static final int ACTION_FIRST The first number in the range of ids used for action events. See Also: [Constant Field Values](../../../constant-values.html#java.awt.event.ActionEvent.ACTION%5FFIRST) * #### ACTION\_LAST public static final int ACTION_LAST The last number in the range of ids used for action events. See Also: [Constant Field Values](../../../constant-values.html#java.awt.event.ActionEvent.ACTION%5FLAST) * #### ACTION\_PERFORMED [@Native](../../../java/lang/annotation/Native.html "annotation in java.lang.annotation") public static final int ACTION_PERFORMED This event id indicates that a meaningful action occurred. See Also: [Constant Field Values](../../../constant-values.html#java.awt.event.ActionEvent.ACTION%5FPERFORMED)
Constructor Detail
* #### ActionEvent public ActionEvent([Object](../../../java/lang/Object.html "class in java.lang") source, int id, [String](../../../java/lang/String.html "class in java.lang") command) Constructs an `ActionEvent` object. This method throws an`IllegalArgumentException` if `source` is `null`. A `null` `command` string is legal, but not recommended. Parameters: `source` \- The object that originated the event `id` \- An integer that identifies the event. For information on allowable values, see the class description for [ActionEvent](../../../java/awt/event/ActionEvent.html "class in java.awt.event") `command` \- A string that may specify a command (possibly one of several) associated with the event Throws: `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if `source` is null See Also: [EventObject.getSource()](../../../java/util/EventObject.html#getSource--), [AWTEvent.getID()](../../../java/awt/AWTEvent.html#getID--), [getActionCommand()](../../../java/awt/event/ActionEvent.html#getActionCommand--) * #### ActionEvent public ActionEvent([Object](../../../java/lang/Object.html "class in java.lang") source, int id, [String](../../../java/lang/String.html "class in java.lang") command, int modifiers) Constructs an `ActionEvent` object with modifier keys. This method throws an`IllegalArgumentException` if `source` is `null`. A `null` `command` string is legal, but not recommended. Parameters: `source` \- The object that originated the event `id` \- An integer that identifies the event. For information on allowable values, see the class description for [ActionEvent](../../../java/awt/event/ActionEvent.html "class in java.awt.event") `command` \- A string that may specify a command (possibly one of several) associated with the event `modifiers` \- The modifier keys down during event (shift, ctrl, alt, meta). Passing negative parameter is not recommended. Zero value means that no modifiers were passed Throws: `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if `source` is null See Also: [EventObject.getSource()](../../../java/util/EventObject.html#getSource--), [AWTEvent.getID()](../../../java/awt/AWTEvent.html#getID--), [getActionCommand()](../../../java/awt/event/ActionEvent.html#getActionCommand--), [getModifiers()](../../../java/awt/event/ActionEvent.html#getModifiers--) * #### ActionEvent public ActionEvent([Object](../../../java/lang/Object.html "class in java.lang") source, int id, [String](../../../java/lang/String.html "class in java.lang") command, long when, int modifiers) Constructs an `ActionEvent` object with the specified modifier keys and timestamp. This method throws an`IllegalArgumentException` if `source` is `null`. A `null` `command` string is legal, but not recommended. Parameters: `source` \- The object that originated the event `id` \- An integer that identifies the event. For information on allowable values, see the class description for [ActionEvent](../../../java/awt/event/ActionEvent.html "class in java.awt.event") `command` \- A string that may specify a command (possibly one of several) associated with the event `modifiers` \- The modifier keys down during event (shift, ctrl, alt, meta). Passing negative parameter is not recommended. Zero value means that no modifiers were passed `when` \- A long that gives the time the event occurred. Passing negative or zero value is not recommended Throws: `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if `source` is null Since: 1.4 See Also: [EventObject.getSource()](../../../java/util/EventObject.html#getSource--), [AWTEvent.getID()](../../../java/awt/AWTEvent.html#getID--), [getActionCommand()](../../../java/awt/event/ActionEvent.html#getActionCommand--), [getModifiers()](../../../java/awt/event/ActionEvent.html#getModifiers--), [getWhen()](../../../java/awt/event/ActionEvent.html#getWhen--)
Method Detail
* #### getActionCommand public [String](../../../java/lang/String.html "class in java.lang") getActionCommand() Returns the command string associated with this action. This string allows a "modal" component to specify one of several commands, depending on its state. For example, a single button might toggle between "show details" and "hide details". The source object and the event would be the same in each case, but the command string would identify the intended action. Note that if a `null` command string was passed to the constructor for this `ActionEvent`, this this method returns `null`. Returns: the string identifying the command for this event * #### getWhen public long getWhen() Returns the timestamp of when this event occurred. Because an ActionEvent is a high-level, semantic event, the timestamp is typically the same as an underlying InputEvent. Returns: this event's timestamp Since: 1.4 * #### getModifiers public int getModifiers() Returns the modifier keys held down during this action event. Returns: the bitwise-or of the modifier constants * #### paramString public [String](../../../java/lang/String.html "class in java.lang") paramString() Returns a parameter string identifying this action event. This method is useful for event-logging and for debugging. Overrides: `[paramString](../../../java/awt/AWTEvent.html#paramString--)` in class `[AWTEvent](../../../java/awt/AWTEvent.html "class in java.awt")` Returns: a string identifying the event and its associated command
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.