Class MouseEvent
- java.lang.Object
-
- java.util.EventObject
-
- java.awt.AWTEvent
-
- java.awt.event.ComponentEvent
-
- java.awt.event.InputEvent
-
- java.awt.event.MouseEvent
-
- All Implemented Interfaces:
- java.io.Serializable
- Direct Known Subclasses:
- MouseWheelEvent
public class MouseEvent extends InputEventAn event which indicates that a mouse action occurred in a component. A mouse action is considered to occur in a particular component if and only if the mouse cursor is over the unobscured part of the component's bounds when the action happens. For lightweight components, such as Swing's components, mouse events are only dispatched to the component if the mouse event type has been enabled on the component. A mouse event type is enabled by adding the appropriate mouse-basedEventListener
to the component (MouseListener
orMouseMotionListener
), or by invokingComponent.enableEvents(long)
with the appropriate mask parameter (AWTEvent.MOUSE_EVENT_MASK
orAWTEvent.MOUSE_MOTION_EVENT_MASK
). If the mouse event type has not been enabled on the component, the corresponding mouse events are dispatched to the first ancestor that has enabled the mouse event type.For example, if a
MouseListener
has been added to a component, orenableEvents(AWTEvent.MOUSE_EVENT_MASK)
has been invoked, then all the events defined byMouseListener
are dispatched to the component. On the other hand, if aMouseMotionListener
has not been added andenableEvents
has not been invoked withAWTEvent.MOUSE_MOTION_EVENT_MASK
, then mouse motion events are not dispatched to the component. Instead the mouse motion events are dispatched to the first ancestors that has enabled mouse motion events.This low-level event is generated by a component object for:
- Mouse Events
- a mouse button is pressed
- a mouse button is released
- a mouse button is clicked (pressed and released)
- the mouse cursor enters the unobscured part of component's geometry
- the mouse cursor exits the unobscured part of component's geometry
- Mouse Motion Events
- the mouse is moved
- the mouse is dragged
A
MouseEvent
object is passed to everyMouseListener
orMouseAdapter
object which is registered to receive the "interesting" mouse events using the component'saddMouseListener
method. (MouseAdapter
objects implement theMouseListener
interface.) Each such listener object gets aMouseEvent
containing the mouse event.A
MouseEvent
object is also passed to everyMouseMotionListener
orMouseMotionAdapter
object which is registered to receive mouse motion events using the component'saddMouseMotionListener
method. (MouseMotionAdapter
objects implement theMouseMotionListener
interface.) Each such listener object gets aMouseEvent
containing the mouse motion event.When a mouse button is clicked, events are generated and sent to the registered
MouseListener
s. The state of modal keys can be retrieved usingInputEvent.getModifiers()
andInputEvent.getModifiersEx()
. The button mask returned byInputEvent.getModifiers()
reflects only the button that changed state, not the current state of all buttons. (Note: Due to overlap in the values of ALT_MASK/BUTTON2_MASK and META_MASK/BUTTON3_MASK, this is not always true for mouse events involving modifier keys). To get the state of all buttons and modifier keys, useInputEvent.getModifiersEx()
. The button which has changed state is returned bygetButton()
For example, if the first mouse button is pressed, events are sent in the following order:
id modifiers button
When multiple mouse buttons are pressed, each press, release, and click results in a separate event.MOUSE_PRESSED
:BUTTON1_MASK
BUTTON1
MOUSE_RELEASED
:BUTTON1_MASK
BUTTON1
MOUSE_CLICKED
:BUTTON1_MASK
BUTTON1
For example, if the user presses button 1 followed by button 2, and then releases them in the same order, the following sequence of events is generated:
id modifiers button
If button 2 is released first, theMOUSE_PRESSED
:BUTTON1_MASK
BUTTON1
MOUSE_PRESSED
:BUTTON2_MASK
BUTTON2
MOUSE_RELEASED
:BUTTON1_MASK
BUTTON1
MOUSE_CLICKED
:BUTTON1_MASK
BUTTON1
MOUSE_RELEASED
:BUTTON2_MASK
BUTTON2
MOUSE_CLICKED
:BUTTON2_MASK
BUTTON2
MOUSE_RELEASED
/MOUSE_CLICKED
pair forBUTTON2_MASK
arrives first, followed by the pair forBUTTON1_MASK
.Some extra mouse buttons are added to extend the standard set of buttons represented by the following constants:
BUTTON1
,BUTTON2
, andBUTTON3
. Extra buttons have no assignedBUTTONx
constants as well as their button masks have no assignedBUTTONx_DOWN_MASK
constants. Nevertheless, ordinal numbers starting from 4 may be used as button numbers (button ids). Values obtained by thegetMaskForButton(button)
method may be used as button masks.MOUSE_DRAGGED
events are delivered to theComponent
in which the mouse button was pressed until the mouse button is released (regardless of whether the mouse position is within the bounds of theComponent
). Due to platform-dependent Drag&Drop implementations,MOUSE_DRAGGED
events may not be delivered during a native Drag&Drop operation. In a multi-screen environment mouse drag events are delivered to theComponent
even if the mouse position is outside the bounds of theGraphicsConfiguration
associated with thatComponent
. However, the reported position for mouse drag events in this case may differ from the actual mouse position:- In a multi-screen environment without a virtual device:
The reported coordinates for mouse drag events are clipped to fit within the bounds of theGraphicsConfiguration
associated with theComponent
. - In a multi-screen environment with a virtual device:
The reported coordinates for mouse drag events are clipped to fit within the bounds of the virtual device associated with theComponent
.
An unspecified behavior will be caused if the
id
parameter of any particularMouseEvent
instance is not in the range fromMOUSE_FIRST
toMOUSE_LAST
-1 (MOUSE_WHEEL
is not acceptable).
-
-
Fields Modifier and Type Field and Description ACTION_EVENT_MASK The event mask for selecting action events.ADJUSTMENT_EVENT_MASK The event mask for selecting adjustment events.ALT_DOWN_MASK The Alt key extended modifier constant.ALT_GRAPH_DOWN_MASK The AltGraph key extended modifier constant.ALT_GRAPH_MASK The AltGraph key modifier constant.ALT_MASK The Alt key modifier constant.BUTTON1 Indicates mouse button #1; used bygetButton()
.BUTTON1_DOWN_MASK The Mouse Button1 extended modifier constant.BUTTON1_MASK The Mouse Button1 modifier constant.BUTTON2 Indicates mouse button #2; used bygetButton()
.BUTTON2_DOWN_MASK The Mouse Button2 extended modifier constant.BUTTON2_MASK The Mouse Button2 modifier constant.BUTTON3 Indicates mouse button #3; used bygetButton()
.BUTTON3_DOWN_MASK The Mouse Button3 extended modifier constant.BUTTON3_MASK The Mouse Button3 modifier constant.COMPONENT_EVENT_MASK The event mask for selecting component events.COMPONENT_FIRST The first number in the range of ids used for component events.COMPONENT_HIDDEN This event indicates that the component was rendered invisible.COMPONENT_LAST The last number in the range of ids used for component events.COMPONENT_MOVED This event indicates that the component's position changed.COMPONENT_RESIZED This event indicates that the component's size changed.COMPONENT_SHOWN This event indicates that the component was made visible.consumed Controls whether or not the event is sent back down to the peer once the source has processed it - false means it's sent to the peer; true means it's not.CONTAINER_EVENT_MASK The event mask for selecting container events.CTRL_DOWN_MASK The Control key extended modifier constant.CTRL_MASK The Control key modifier constant.FOCUS_EVENT_MASK The event mask for selecting focus events.HIERARCHY_BOUNDS_EVENT_MASK The event mask for selecting hierarchy bounds events.HIERARCHY_EVENT_MASK The event mask for selecting hierarchy events.id The event's id.INPUT_METHOD_EVENT_MASK The event mask for selecting input method events.INVOCATION_EVENT_MASK The event mask for selecting invocation events.ITEM_EVENT_MASK The event mask for selecting item events.KEY_EVENT_MASK The event mask for selecting key events.META_DOWN_MASK The Meta key extended modifier constant.META_MASK The Meta key modifier constant.MOUSE_CLICKED The "mouse clicked" event.MOUSE_DRAGGED The "mouse dragged" event.MOUSE_ENTERED The "mouse entered" event.MOUSE_EVENT_MASK The event mask for selecting mouse events.MOUSE_EXITED The "mouse exited" event.MOUSE_FIRST The first number in the range of ids used for mouse events.MOUSE_LAST The last number in the range of ids used for mouse events.MOUSE_MOTION_EVENT_MASK The event mask for selecting mouse motion events.MOUSE_MOVED The "mouse moved" event.MOUSE_PRESSED The "mouse pressed" event.MOUSE_RELEASED The "mouse released" event.MOUSE_WHEEL The "mouse wheel" event.MOUSE_WHEEL_EVENT_MASK The event mask for selecting mouse wheel events.NOBUTTON Indicates no mouse buttons; used bygetButton()
.PAINT_EVENT_MASK The event mask for selecting paint events.RESERVED_ID_MAX The maximum value for reserved AWT event IDs.SHIFT_DOWN_MASK The Shift key extended modifier constant.SHIFT_MASK The Shift key modifier constant.source The object on which the Event initially occurred.TEXT_EVENT_MASK The event mask for selecting text events.WINDOW_EVENT_MASK The event mask for selecting window events.WINDOW_FOCUS_EVENT_MASK The event mask for selecting window focus events.WINDOW_STATE_EVENT_MASK The event mask for selecting window state events.
-
Static Methods Modifier and Type Static Method and Description getMaskForButton A method to obtain a mask for any existing mouse button.getModifiersExText Returns a String describing the extended modifier keys and mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift".getMouseModifiersText Returns aString
instance describing the modifier keys and mouse buttons that were down during the event, such as "Shift", or "Ctrl+Shift".
-
Methods Modifier and Type Method and Description consume Consumes this event so that it will not be processed in the default manner by the source which originated it.getButton Returns which, if any, of the mouse buttons has changed state.getClickCount Returns the number of mouse clicks associated with this event.getComponent Returns the originator of the event.getID Returns the event type.getLocationOnScreen Returns the absolute x, y position of the event.getModifiers Returns the modifier mask for this event.getModifiersEx Returns the extended modifier mask for this event.getPoint Returns the x,y position of the event relative to the source component.getSource The object on which the Event initially occurred.getWhen Returns the difference in milliseconds between the timestamp of when this event occurred and midnight, January 1, 1970 UTC.getX Returns the horizontal x position of the event relative to the source component.getXOnScreen Returns the absolute horizontal x position of the event.getY Returns the vertical y position of the event relative to the source component.getYOnScreen Returns the absolute vertical y position of the event.isAltDown Returns whether or not the Alt modifier is down on this event.isAltGraphDown Returns whether or not the AltGraph modifier is down on this event.isConsumed Returns whether or not this event has been consumed.isControlDown Returns whether or not the Control modifier is down on this event.isMetaDown Returns whether or not the Meta modifier is down on this event.isPopupTrigger Returns whether or not this mouse event is the popup menu trigger event for the platform.isShiftDown Returns whether or not the Shift modifier is down on this event.paramString Returns a parameter string identifying this event.setSource Retargets an event to a new source.toString Returns a String representation of this object.translatePoint Translates the event's coordinates to a new position by adding specifiedx
(horizontal) andy
(vertical) offsets.
-