GeezerVison is showing docs.oracle.com — MouseEvent (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
java.awt.event
Class MouseEvent
-
-
Field Summary
-
Fields inherited from class java.awt.event.InputEvent
ALT_DOWN_MASK, ALT_GRAPH_DOWN_MASK, ALT_GRAPH_MASK, ALT_MASK, BUTTON1_DOWN_MASK, BUTTON1_MASK, BUTTON2_DOWN_MASK, BUTTON2_MASK, BUTTON3_DOWN_MASK, BUTTON3_MASK, CTRL_DOWN_MASK, CTRL_MASK, META_DOWN_MASK, META_MASK, SHIFT_DOWN_MASK, SHIFT_MASK
-
Fields inherited from class java.awt.AWTEvent
ACTION_EVENT_MASK, ADJUSTMENT_EVENT_MASK, COMPONENT_EVENT_MASK, consumed, CONTAINER_EVENT_MASK, FOCUS_EVENT_MASK, HIERARCHY_BOUNDS_EVENT_MASK, HIERARCHY_EVENT_MASK, id, INPUT_METHOD_EVENT_MASK, INVOCATION_EVENT_MASK, ITEM_EVENT_MASK, KEY_EVENT_MASK, MOUSE_EVENT_MASK, MOUSE_MOTION_EVENT_MASK, MOUSE_WHEEL_EVENT_MASK, PAINT_EVENT_MASK, RESERVED_ID_MAX, TEXT_EVENT_MASK, WINDOW_EVENT_MASK, WINDOW_FOCUS_EVENT_MASK, WINDOW_STATE_EVENT_MASK
-
Constructor Summary
Constructors
| Constructor |
Description |
MouseEvent(Component source,
int id,
long when,
int modifiers,
int x,
int y,
int clickCount,
boolean popupTrigger) |
Constructs a MouseEvent object with the
specified source component,
type, modifiers, coordinates, click count, and popupTrigger flag.
|
MouseEvent(Component source,
int id,
long when,
int modifiers,
int x,
int y,
int clickCount,
boolean popupTrigger,
int button) |
Constructs a MouseEvent object with the
specified source component,
type, time, modifiers, coordinates, click count, popupTrigger flag,
and button number.
|
MouseEvent(Component source,
int id,
long when,
int modifiers,
int x,
int y,
int xAbs,
int yAbs,
int clickCount,
boolean popupTrigger,
int button) |
Constructs a MouseEvent object with the
specified source component,
type, time, modifiers, coordinates, absolute coordinates, click count, popupTrigger flag,
and button number.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
int |
getButton() |
Returns which, if any, of the mouse buttons has changed state.
|
int |
getClickCount() |
Returns the number of mouse clicks associated with this event.
|
Point |
getLocationOnScreen() |
Returns the absolute x, y position of the event.
|
int |
getModifiersEx() |
Returns the extended modifier mask for this event.
|
static String |
getMouseModifiersText(int modifiers) |
Returns a String instance describing the modifier keys and
mouse buttons that were down during the event, such as "Shift",
or "Ctrl+Shift".
|
Point |
getPoint() |
Returns the x,y position of the event relative to the source component.
|
int |
getX() |
Returns the horizontal x position of the event relative to the
source component.
|
int |
getXOnScreen() |
Returns the absolute horizontal x position of the event.
|
int |
getY() |
Returns the vertical y position of the event relative to the
source component.
|
int |
getYOnScreen() |
Returns the absolute vertical y position of the event.
|
boolean |
isPopupTrigger() |
Returns whether or not this mouse event is the popup menu
trigger event for the platform.
|
String |
paramString() |
Returns a parameter string identifying this event.
|
void |
translatePoint(int x,
int y) |
Translates the event's coordinates to a new position
by adding specified x (horizontal) and y
(vertical) offsets.
|
-
Methods inherited from class java.awt.event.InputEvent
consume, getMaskForButton, getModifiers, getModifiersExText, getWhen, isAltDown, isAltGraphDown, isConsumed, isControlDown, isMetaDown, isShiftDown
-
-
Constructor Detail
-
MouseEvent
public MouseEvent(Component source,
int id,
long when,
int modifiers,
int x,
int y,
int clickCount,
boolean popupTrigger,
int button)
Constructs a MouseEvent object with the
specified source component,
type, time, modifiers, coordinates, click count, popupTrigger flag,
and button number.
Creating an invalid event (such
as by using more than one of the old _MASKs, or modifier/button
values which don't match) results in unspecified behavior.
An invocation of the form
MouseEvent(source, id, when, modifiers, x, y, clickCount, popupTrigger, button)
behaves in exactly the same way as the invocation
MouseEvent(source, id, when, modifiers,
x, y, xAbs, yAbs, clickCount, popupTrigger, button)
where xAbs and yAbs defines as source's location on screen plus
relative coordinates x and y.
xAbs and yAbs are set to zero if the source is not showing.
This method throws an
IllegalArgumentException if source
is null.
- Parameters:
source - The Component that originated the event
id - An integer indicating the type of event.
For information on allowable values, see
the class description for MouseEvent
when - A long integer that gives the time the event occurred.
Passing negative or zero value
is not recommended
modifiers - a modifier mask describing the modifier keys and mouse
buttons (for example, shift, ctrl, alt, and meta) that
are down during the event.
Only extended modifiers are allowed to be used as a
value for this parameter (see the InputEvent.getModifiersEx()
class for the description of extended modifiers).
Passing negative parameter
is not recommended.
Zero value means that no modifiers were passed
x - The horizontal x coordinate for the mouse location.
It is allowed to pass negative values
y - The vertical y coordinate for the mouse location.
It is allowed to pass negative values
clickCount - The number of mouse clicks associated with event.
Passing negative value
is not recommended
popupTrigger - A boolean that equals true if this event
is a trigger for a popup menu
button - An integer that indicates, which of the mouse buttons has
changed its state.
The following rules are applied to this parameter:
- If support for the extended mouse buttons is
disabled by Java
then it is allowed to create MouseEvent objects only with the standard buttons:
NOBUTTON, BUTTON1, BUTTON2, and
BUTTON3.
- If support for the extended mouse buttons is
enabled by Java
then it is allowed to create MouseEvent objects with
the standard buttons.
In case the support for extended mouse buttons is
enabled by Java, then
in addition to the standard buttons, MouseEvent objects can be created
using buttons from the range starting from 4 to
MouseInfo.getNumberOfButtons()
if the mouse has more than three buttons.
- Throws:
IllegalArgumentException - if button is less then zero
IllegalArgumentException - if source is null
IllegalArgumentException - if button is greater then BUTTON3 and the support for extended mouse buttons is
disabled by Java
IllegalArgumentException - if button is greater then the
current number of buttons and the support
for extended mouse buttons is enabled
by Java
IllegalArgumentException - if an invalid button
value is passed in
IllegalArgumentException - if source is null
- Since:
- 1.4
- See Also:
EventObject.getSource(),
AWTEvent.getID(),
InputEvent.getWhen(),
InputEvent.getModifiers(),
getX(),
getY(),
getClickCount(),
isPopupTrigger(),
getButton()
-
MouseEvent
public MouseEvent(Component source,
int id,
long when,
int modifiers,
int x,
int y,
int clickCount,
boolean popupTrigger)
Constructs a MouseEvent object with the
specified source component,
type, modifiers, coordinates, click count, and popupTrigger flag.
An invocation of the form
MouseEvent(source, id, when, modifiers, x, y, clickCount, popupTrigger)
behaves in exactly the same way as the invocation
MouseEvent(source, id, when, modifiers,
x, y, xAbs, yAbs, clickCount, popupTrigger, MouseEvent.NOBUTTON)
where xAbs and yAbs defines as source's location on screen plus
relative coordinates x and y.
xAbs and yAbs are set to zero if the source is not showing.
This method throws an IllegalArgumentException
if source is null.
- Parameters:
source - The Component that originated the event
id - An integer indicating the type of event.
For information on allowable values, see
the class description for MouseEvent
when - A long integer that gives the time the event occurred.
Passing negative or zero value
is not recommended
modifiers - a modifier mask describing the modifier keys and mouse
buttons (for example, shift, ctrl, alt, and meta) that
are down during the event.
Only extended modifiers are allowed to be used as a
value for this parameter (see the InputEvent.getModifiersEx()
class for the description of extended modifiers).
Passing negative parameter
is not recommended.
Zero value means that no modifiers were passed
x - The horizontal x coordinate for the mouse location.
It is allowed to pass negative values
y - The vertical y coordinate for the mouse location.
It is allowed to pass negative values
clickCount - The number of mouse clicks associated with event.
Passing negative value
is not recommended
popupTrigger - A boolean that equals true if this event
is a trigger for a popup menu
- Throws:
IllegalArgumentException - if source is null
- See Also:
EventObject.getSource(),
AWTEvent.getID(),
InputEvent.getWhen(),
InputEvent.getModifiers(),
getX(),
getY(),
getClickCount(),
isPopupTrigger()
-
MouseEvent
public MouseEvent(Component source,
int id,
long when,
int modifiers,
int x,
int y,
int xAbs,
int yAbs,
int clickCount,
boolean popupTrigger,
int button)
Constructs a MouseEvent object with the
specified source component,
type, time, modifiers, coordinates, absolute coordinates, click count, popupTrigger flag,
and button number.
Creating an invalid event (such
as by using more than one of the old _MASKs, or modifier/button
values which don't match) results in unspecified behavior.
Even if inconsistent values for relative and absolute coordinates are
passed to the constructor, the mouse event instance is still
created and no exception is thrown.
This method throws an
IllegalArgumentException if source
is null.
- Parameters:
source - The Component that originated the event
id - An integer indicating the type of event.
For information on allowable values, see
the class description for MouseEvent
when - A long integer that gives the time the event occurred.
Passing negative or zero value
is not recommended
modifiers - a modifier mask describing the modifier keys and mouse
buttons (for example, shift, ctrl, alt, and meta) that
are down during the event.
Only extended modifiers are allowed to be used as a
value for this parameter (see the InputEvent.getModifiersEx()
class for the description of extended modifiers).
Passing negative parameter
is not recommended.
Zero value means that no modifiers were passed
x - The horizontal x coordinate for the mouse location.
It is allowed to pass negative values
y - The vertical y coordinate for the mouse location.
It is allowed to pass negative values
xAbs - The absolute horizontal x coordinate for the mouse location
It is allowed to pass negative values
yAbs - The absolute vertical y coordinate for the mouse location
It is allowed to pass negative values
clickCount - The number of mouse clicks associated with event.
Passing negative value
is not recommended
popupTrigger - A boolean that equals true if this event
is a trigger for a popup menu
button - An integer that indicates, which of the mouse buttons has
changed its state.
The following rules are applied to this parameter:
- If support for the extended mouse buttons is
disabled by Java
then it is allowed to create MouseEvent objects only with the standard buttons:
NOBUTTON, BUTTON1, BUTTON2, and
BUTTON3.
- If support for the extended mouse buttons is
enabled by Java
then it is allowed to create MouseEvent objects with
the standard buttons.
In case the support for extended mouse buttons is
enabled by Java, then
in addition to the standard buttons, MouseEvent objects can be created
using buttons from the range starting from 4 to
MouseInfo.getNumberOfButtons()
if the mouse has more than three buttons.
- Throws:
IllegalArgumentException - if button is less then zero
IllegalArgumentException - if source is null
IllegalArgumentException - if button is greater then BUTTON3 and the support for extended mouse buttons is
disabled by Java
IllegalArgumentException - if button is greater then the
current number of buttons and the support
for extended mouse buttons is enabled
by Java
IllegalArgumentException - if an invalid button
value is passed in
IllegalArgumentException - if source is null
- Since:
- 1.6
- See Also:
EventObject.getSource(),
AWTEvent.getID(),
InputEvent.getWhen(),
InputEvent.getModifiers(),
getX(),
getY(),
getXOnScreen(),
getYOnScreen(),
getClickCount(),
isPopupTrigger(),
getButton(),
button,
Toolkit.areExtraMouseButtonsEnabled(),
MouseInfo.getNumberOfButtons(),
InputEvent.getMaskForButton(int)
-
Method Detail
-
getLocationOnScreen
public Point getLocationOnScreen()
Returns the absolute x, y position of the event.
In a virtual device multi-screen environment in which the
desktop area could span multiple physical screen devices,
these coordinates are relative to the virtual coordinate system.
Otherwise, these coordinates are relative to the coordinate system
associated with the Component's GraphicsConfiguration.
- Returns:
- a
Point object containing the absolute x
and y coordinates.
- Since:
- 1.6
- See Also:
GraphicsConfiguration
-
getXOnScreen
public int getXOnScreen()
Returns the absolute horizontal x position of the event.
In a virtual device multi-screen environment in which the
desktop area could span multiple physical screen devices,
this coordinate is relative to the virtual coordinate system.
Otherwise, this coordinate is relative to the coordinate system
associated with the Component's GraphicsConfiguration.
- Returns:
- x an integer indicating absolute horizontal position.
- Since:
- 1.6
- See Also:
GraphicsConfiguration
-
getYOnScreen
public int getYOnScreen()
Returns the absolute vertical y position of the event.
In a virtual device multi-screen environment in which the
desktop area could span multiple physical screen devices,
this coordinate is relative to the virtual coordinate system.
Otherwise, this coordinate is relative to the coordinate system
associated with the Component's GraphicsConfiguration.
- Returns:
- y an integer indicating absolute vertical position.
- Since:
- 1.6
- See Also:
GraphicsConfiguration
-
translatePoint
public void translatePoint(int x,
int y)
Translates the event's coordinates to a new position
by adding specified x (horizontal) and y
(vertical) offsets.
- Parameters:
x - the horizontal x value to add to the current x
coordinate position
y - the vertical y value to add to the current y
coordinate position
-
getButton
public int getButton()
Returns which, if any, of the mouse buttons has changed state.
The returned value is ranged
from 0 to the MouseInfo.getNumberOfButtons()
value.
The returned value includes at least the following constants:
-
NOBUTTON
-
BUTTON1
-
BUTTON2
-
BUTTON3
It is allowed to use those constants to compare with the returned button number in the application.
For example,
if (anEvent.getButton() == MouseEvent.BUTTON1) {
In particular, for a mouse with one, two, or three buttons this method may return the following values:
- 0 (
NOBUTTON)
- 1 (
BUTTON1)
- 2 (
BUTTON2)
- 3 (
BUTTON3)
Button numbers greater then BUTTON3 have no constant identifier. So if a mouse with five buttons is
installed, this method may return the following values:
- 0 (
NOBUTTON)
- 1 (
BUTTON1)
- 2 (
BUTTON2)
- 3 (
BUTTON3)
- 4
- 5
Note: If support for extended mouse buttons is disabled by Java
then the AWT event subsystem does not produce mouse events for the extended mouse
buttons. So it is not expected that this method returns anything except NOBUTTON, BUTTON1,
BUTTON2, BUTTON3.
- Returns:
- one of the values from 0 to
MouseInfo.getNumberOfButtons()
if support for the extended mouse buttons is enabled by Java.
That range includes NOBUTTON, BUTTON1, BUTTON2, BUTTON3;
NOBUTTON, BUTTON1, BUTTON2 or BUTTON3
if support for the extended mouse buttons is disabled by Java
- Since:
- 1.4
- See Also:
Toolkit.areExtraMouseButtonsEnabled(),
MouseInfo.getNumberOfButtons(),
MouseEvent(Component, int, long, int, int, int, int, int, int, boolean, int),
InputEvent.getMaskForButton(int)
Java™ Platform Standard Ed. 8
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, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|