Class MouseEvent
- All Implemented Interfaces:
-
Serializable,Cloneable
- Direct Known Subclasses:
MouseDragEvent
public class MouseEvent extends InputEvent
EventDispatcher. The mouse (pointer's) location is available relative to several coordinate systems: x,y - relative to the origin of the MouseEvent's node, sceneX,sceneY - relative to to the origin of the Scene that contains the node, screenX,screenY - relative to origin of the screen that contains the mouse pointer.
Dragging gestures
There are three types of dragging gestures. They are all initiated by a mouse press event and terminated as a result of a mouse released event, the source node decides which gesture will take place.
The simple press-drag-release gesture is the default. It's best used to allow changing the size of a shape, dragging it around and so on. The whole press-drag-release gesture is delivered to one node. When a mouse button is pressed, the top-most node is picked and all subsequent mouse events are delivered to that same node until the button is released. If a mouse clicked event is generated from these events, it is still delivered to the same node.
During a simple press-drag-release gesture, the other nodes are not involved and don't get any events. If these nodes need to be involved in the gesture, full press-drag-release gesture has to be activated. This gesture is best used for connecting nodes by "wires", dragging nodes to other nodes etc. This gesture type is more closely described at MouseDragEvent which contains the events delivered to the gesture targets.
The third gesture type is the platform-supported drag-and-drop gesture. It serves best to transfer data and works also between (not necessarily FX) applications. This gesture type is more closely described at DragEvent.
In summary, simple press-drag-release gestures are activated automatically when a mouse button is pressed and delivers all MouseEvents to the gesture source. When you start dragging, eventually the DRAG_DETECTED event arrives. In its handler, you can either start a full press-drag-release gesture by calling the startFullDrag method on a node or scene - the MouseDragEvents start to be delivered to gesture targets, or you can start drag and drop gesture by calling startDragAndDrop method on a node or scene - the system switches into the drag and drop mode and DragEvents start to be delivered instead of MouseEvents. If you don't call any of those methods, the simple press-drag-release gesture continues.
Note that dragging a finger over touch screen produces mouse dragging events, but also scroll gesture events. If it means a conflict in an application (the physical dragging action is handled by two different handlers), the isSynthesized() method may be used to detect the problem and make the dragging handlers behave accordingly.
Mouse enter/exit handling
When the mouse enters a node, the node gets a MOUSE_ENTERED event, when it leaves, it gets a MOUSE_EXITED event. These events are delivered only to the entered/exited node and seemingly don't go through the capturing/bubbling phases. This is the most common use-case.
When capturing or bubbling is desired, there are MOUSE_ENTERED_TARGET/MOUSE_EXITED_TARGET events. These events go through capturing/bubbling phases normally. This means that a parent may receive the MOUSE_ENTERED_TARGET event when the mouse entered either the parent itself or some of its children. To distinguish between these two cases, the event target can be tested on equality with the node.
These two types are closely connected: MOUSE_ENTERED/MOUSE_EXITED are subtypes of MOUSE_ENTERED_TARGET/MOUSE_EXITED_TARGET. During capturing phase, MOUSE_ENTERED_TARGET is delivered to the parents. When the event is delivered to the event target (the node that has actually been entered), its type is switched to MOUSE_ENTERED. Then the type is switched back to MOUSE_ENTERED_TARGET for the bubbling phase. It's still one event just switching types, so if it's filtered or consumed, it affects both event variants. Thanks to the subtype-relationship, a MOUSE_ENTERED_TARGET event handler will receive the MOUSE_ENTERED event on target.
Notes
- For triggering context menus see the
ContextMenuEvent.
- Since:
- JavaFX 2.0
- See Also:
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
static final EventType |
ANY |
Common supertype for all mouse event types. |
static final EventType |
DRAG_DETECTED |
This event is delivered to a node that is identified as a source of a dragging gesture. |
static final EventType |
MOUSE_CLICKED |
This event occurs when a mouse button has been clicked (pressed and released on the same node). |
static final EventType |
MOUSE_DRAGGED |
This event occurs when the mouse moves with a pressed button. |
static final EventType |
MOUSE_ENTERED |
This event occurs when the mouse enters a node. |
static final EventType |
MOUSE_ENTERED_TARGET |
This event occurs when the mouse enters a node. |
static final EventType |
MOUSE_EXITED |
This event occurs when the mouse exits a node. |
static final EventType |
MOUSE_EXITED_TARGET |
This event occurs when the mouse exits a node. |
static final EventType |
MOUSE_MOVED |
This event occurs when the mouse moves within a node and no buttons are pressed. |
static final EventType |
MOUSE_PRESSED |
This event occurs when a mouse button is pressed. |
static final EventType |
MOUSE_RELEASED |
This event occurs when a mouse button is released. |
Fields declared in class javafx.event.Event
consumed, eventType, NULL_SOURCE_TARGET, target
Fields declared in class java.util.EventObject
source
Constructor Summary
| Constructor | Description |
|---|---|
MouseEvent |
Constructs new MouseEvent event. |
MouseEvent |
Constructs new MouseEvent event. |
MouseEvent |
Constructs new MouseEvent event with null source and target. |
MouseEvent |
Constructs new MouseEvent event with null source and target. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
MouseEvent |
copyFor |
Copies this event for a different source and target. |
MouseEvent |
copyFor |
Creates a copy of the given event with the given fields substituted. |
static MouseDragEvent |
copyForMouseDragEvent |
Creates a copy of this mouse event of MouseDragEvent type |
final MouseButton |
getButton() |
Which, if any, of the mouse buttons is responsible for this event. |
final int |
getClickCount() |
Returns number of mouse clicks associated with this event. |
EventType |
getEventType() |
Gets the event type of this event. |
final PickResult |
getPickResult() |
Returns information about the pick. |
final double |
getSceneX() |
Returns horizontal position of the event relative to the origin of the Scene that contains the MouseEvent's source. |
final double |
getSceneY() |
Returns vertical position of the event relative to the origin of the Scene that contains the MouseEvent's source. |
final double |
getScreenX() |
Returns absolute horizontal position of the event. |
final double |
getScreenY() |
Returns absolute vertical position of the event. |
final double |
getX() |
Horizontal position of the event relative to the origin of the MouseEvent's source. |
final double |
getY() |
Vertical position of the event relative to the origin of the MouseEvent's source. |
final double |
getZ() |
Depth position of the event relative to the origin of the MouseEvent's source. |
final boolean |
isAltDown() |
Whether or not the Alt modifier is down on this event. |
final boolean |
isBackButtonDown() |
Returns true if back button (button 4) is currently pressed. |
final boolean |
isControlDown() |
Whether or not the Control modifier is down on this event. |
boolean |
isDragDetect() |
Determines whether this event will be followed by DRAG_DETECTED event. |
final boolean |
isForwardButtonDown() |
Returns true if forward button (button 5) is currently pressed. |
final boolean |
isMetaDown() |
Whether or not the Meta modifier is down on this event. |
final boolean |
isMiddleButtonDown() |
Returns true if middle button (button 2) is currently pressed. |
final boolean |
isPopupTrigger() |
Returns true if this mouse event is the popup menu trigger event for the platform. |
final boolean |
isPrimaryButtonDown() |
Returns true if primary button (button 1, usually the left) is currently pressed. |
final boolean |
isSecondaryButtonDown() |
Returns true if secondary button (button 3, usually the right) is currently pressed. |
final boolean |
isShiftDown() |
Whether or not the Shift modifier is down on this event. |
final boolean |
isShortcutDown() |
Returns whether or not the host platform common shortcut modifier is down on this event. |
final boolean |
isStillSincePress() |
Indicates whether the mouse cursor stayed in the system-provided hysteresis area since last pressed event that occurred before this event. |
boolean |
isSynthesized() |
Indicates whether this event is synthesized from using a touch screen instead of usual mouse event source devices like mouse or track pad. |
void |
setDragDetect |
Augments drag detection behavior. |
String |
toString() |
Returns a string representation of this MouseEvent object. |
Methods declared in class java.util.EventObject
getSource
Field Details
ANY
public static final EventType<MouseEvent> ANY
MOUSE_PRESSED
public static final EventType<MouseEvent> MOUSE_PRESSED
MOUSE_RELEASED
public static final EventType<MouseEvent> MOUSE_RELEASED
MOUSE_CLICKED
public static final EventType<MouseEvent> MOUSE_CLICKED
MOUSE_ENTERED_TARGET
public static final EventType<MouseEvent> MOUSE_ENTERED_TARGET
MOUSE_ENTERED event handler should be used.- See Also:
MOUSE_ENTERED
public static final EventType<MouseEvent> MOUSE_ENTERED
MOUSE_ENTERED_TARGET.- See Also:
MOUSE_EXITED_TARGET
public static final EventType<MouseEvent> MOUSE_EXITED_TARGET
MOUSE_EXITED event handler should be used.- See Also:
MOUSE_EXITED
public static final EventType<MouseEvent> MOUSE_EXITED
MOUSE_EXITED_TARGET.- See Also:
MOUSE_MOVED
public static final EventType<MouseEvent> MOUSE_MOVED
MOUSE_DRAGGED
public static final EventType<MouseEvent> MOUSE_DRAGGED
DRAG_DETECTED
public static final EventType<MouseEvent> DRAG_DETECTED
startFullDrag() of startDragAndDrop() method). If none of them is called, simple press-drag-release gesture will continue. Note that his event is generated based on dragging the mouse over a platform-specific distance threshold. You can modify this behavior by calling setDragDetect method on any MOUSE_PRESSED or MOUSE_DRAGGED event.
- See Also:
Constructor Details
MouseEvent
public MouseEvent(EventType<? extends MouseEvent> eventType, double x, double y, double screenX, double screenY, MouseButton button, int clickCount, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown, boolean primaryButtonDown, boolean middleButtonDown, boolean secondaryButtonDown, boolean synthesized, boolean popupTrigger, boolean stillSincePress, PickResult pickResult)
isBackButtonDown() and isForwardButtonDown() will return false for this event.- Parameters:
-
eventType- The type of the event. -
x- The x with respect to the scene. -
y- The y with respect to the scene. -
screenX- The x coordinate relative to screen. -
screenY- The y coordinate relative to screen. -
button- the mouse button used -
clickCount- number of click counts -
shiftDown- true if shift modifier was pressed. -
controlDown- true if control modifier was pressed. -
altDown- true if alt modifier was pressed. -
metaDown- true if meta modifier was pressed. -
primaryButtonDown- true if primary button was pressed. -
middleButtonDown- true if middle button was pressed. -
secondaryButtonDown- true if secondary button was pressed. -
synthesized- if this event was synthesized -
popupTrigger- whether this event denotes a popup trigger for current platform -
stillSincePress- seeisStillSincePress() -
pickResult- pick result. Can be null, in this case a 2D pick result without any further values is constructed based on the scene coordinates - Since:
- JavaFX 8.0
MouseEvent
public MouseEvent(EventType<? extends MouseEvent> eventType, double x, double y, double screenX, double screenY, MouseButton button, int clickCount, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown, boolean primaryButtonDown, boolean middleButtonDown, boolean secondaryButtonDown, boolean backButtonDown, boolean forwardButtonDown, boolean synthesized, boolean popupTrigger, boolean stillSincePress, PickResult pickResult)
- Parameters:
-
eventType- The type of the event. -
x- The x with respect to the scene. -
y- The y with respect to the scene. -
screenX- The x coordinate relative to screen. -
screenY- The y coordinate relative to screen. -
button- the mouse button used -
clickCount- number of click counts -
shiftDown- true if shift modifier was pressed. -
controlDown- true if control modifier was pressed. -
altDown- true if alt modifier was pressed. -
metaDown- true if meta modifier was pressed. -
primaryButtonDown- true if primary button was pressed. -
middleButtonDown- true if middle button was pressed. -
secondaryButtonDown- true if secondary button was pressed. -
backButtonDown- true if back button was pressed. -
forwardButtonDown- true if forward button was pressed -
synthesized- if this event was synthesized -
popupTrigger- whether this event denotes a popup trigger for current platform -
stillSincePress- seeisStillSincePress() -
pickResult- pick result. Can be null, in this case a 2D pick result without any further values is constructed based on the scene coordinates - Since:
- 12
MouseEvent
public MouseEvent(Object source, EventTarget target, EventType<? extends MouseEvent> eventType, double x, double y, double screenX, double screenY, MouseButton button, int clickCount, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown, boolean primaryButtonDown, boolean middleButtonDown, boolean secondaryButtonDown, boolean synthesized, boolean popupTrigger, boolean stillSincePress, PickResult pickResult)
isBackButtonDown() and isForwardButtonDown() will return false for this event.- Parameters:
-
source- the source of the event. Can be null. -
target- the target of the event. Can be null. -
eventType- The type of the event. -
x- The x with respect to the source. Should be in scene coordinates if source == null or source is not a Node. -
y- The y with respect to the source. Should be in scene coordinates if source == null or source is not a Node. -
screenX- The x coordinate relative to screen. -
screenY- The y coordinate relative to screen. -
button- the mouse button used -
clickCount- number of click counts -
shiftDown- true if shift modifier was pressed. -
controlDown- true if control modifier was pressed. -
altDown- true if alt modifier was pressed. -
metaDown- true if meta modifier was pressed. -
primaryButtonDown- true if primary button was pressed. -
middleButtonDown- true if middle button was pressed. -
secondaryButtonDown- true if secondary button was pressed. -
synthesized- if this event was synthesized -
popupTrigger- whether this event denotes a popup trigger for current platform -
stillSincePress- seeisStillSincePress() -
pickResult- pick result. Can be null, in this case a 2D pick result without any further values is constructed based on the scene coordinates and target - Since:
- JavaFX 8.0
MouseEvent
public MouseEvent(Object source, EventTarget target, EventType<? extends MouseEvent> eventType, double x, double y, double screenX, double screenY, MouseButton button, int clickCount, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown, boolean primaryButtonDown, boolean middleButtonDown, boolean secondaryButtonDown, boolean backButtonDown, boolean forwardButtonDown, boolean synthesized, boolean popupTrigger, boolean stillSincePress, PickResult pickResult)
- Parameters:
-
source- the source of the event. Can be null. -
target- the target of the event. Can be null. -
eventType- The type of the event. -
x- The x with respect to the source. Should be in scene coordinates if source == null or source is not a Node. -
y- The y with respect to the source. Should be in scene coordinates if source == null or source is not a Node. -
screenX- The x coordinate relative to screen. -
screenY- The y coordinate relative to screen. -
button- the mouse button used -
clickCount- number of click counts -
shiftDown- true if shift modifier was pressed. -
controlDown- true if control modifier was pressed. -
altDown- true if alt modifier was pressed. -
metaDown- true if meta modifier was pressed. -
primaryButtonDown- true if primary button was pressed. -
middleButtonDown- true if middle button was pressed. -
secondaryButtonDown- true if secondary button was pressed. -
backButtonDown- true if the back button was pressed -
forwardButtonDown- true if the forward button was pressed -
synthesized- if this event was synthesized -
popupTrigger- whether this event denotes a popup trigger for current platform -
stillSincePress- seeisStillSincePress() -
pickResult- pick result. Can be null, in this case a 2D pick result without any further values is constructed based on the scene coordinates and target - Since:
- 12
Method Details
getEventType
public EventType<? extends MouseEvent> getEventType()
EventEvent class can have different event types. These event types further specify what kind of event occurred.- Overrides:
-
getEventTypein classEvent - Returns:
- the event type
copyFor
public MouseEvent copyFor(Object newSource, EventTarget newTarget)
copyFor
public MouseEvent copyFor(Object newSource, EventTarget newTarget, EventType<? extends MouseEvent> eventType)
- Parameters:
-
newSource- the new source of the copied event -
newTarget- the new target of the copied event -
eventType- the new eventType - Returns:
- the event copy with the fields substituted
- Since:
- JavaFX 8.0
copyForMouseDragEvent
public static MouseDragEvent copyForMouseDragEvent(MouseEvent e, Object source, EventTarget target, EventType<MouseDragEvent> type, Object gestureSource, PickResult pickResult)
- Parameters:
-
e- the mouse event to copy -
source- the new source of the copied event -
target- the new target of the copied event -
type- the new MouseDragEvent type -
gestureSource- the new source of the gesture -
pickResult- pick result. Can be null, in this case a 2D pick result without any further values is constructed based on the scene coordinates - Returns:
- new MouseDragEvent that was created from MouseEvent
- Since:
- JavaFX 8.0
isDragDetect
public boolean isDragDetect()
DRAG_DETECTED event. It has effect only with MOUSE_PRESSED and MOUSE_DRAGGED events.- Returns:
- true if the
DRAG_DETECTEDevent will follow
setDragDetect
public void setDragDetect(boolean dragDetect)
DRAG_DETECTED event. It has effect only with MOUSE_PRESSED and MOUSE_DRAGGED events.- Parameters:
-
dragDetect- WhetherDRAG_DETECTEDevent will follow
getX
public final double getX()
- Returns:
- horizontal position of the event relative to the origin of the MouseEvent's source.
getY
public final double getY()
- Returns:
- vertical position of the event relative to the origin of the MouseEvent's source.
getZ
public final double getZ()
- Returns:
- depth position of the event relative to the origin of the MouseEvent's source.
- Since:
- JavaFX 8.0
getScreenX
public final double getScreenX()
- Returns:
- absolute horizontal position of the event
getScreenY
public final double getScreenY()
- Returns:
- absolute vertical position of the event
getSceneX
public final double getSceneX()
Scene that contains the MouseEvent's source. If the node is not in a Scene, then the value is relative to the boundsInParent of the root-most parent of the MouseEvent's node. Note that in 3D scene, this represents the flat coordinates after applying the projection transformations.- Returns:
- horizontal position of the event relative to the origin of the
Scenethat contains the MouseEvent's source
getSceneY
public final double getSceneY()
Scene that contains the MouseEvent's source. If the node is not in a Scene, then the value is relative to the boundsInParent of the root-most parent of the MouseEvent's node. Note that in 3D scene, this represents the flat coordinates after applying the projection transformations.- Returns:
- vertical position of the event relative to the origin of the
Scenethat contains the MouseEvent's source
getButton
public final MouseButton getButton()
- Returns:
- mouse button whose state change caused this event
getClickCount
public final int getClickCount()
- Returns:
- number of mouse clicks associated with this event
isStillSincePress
public final boolean isStillSincePress()
Click event is generated for a node if mouse was both pressed and released over the node, regardless of mouse movements between the press and release. If a node wants to react differently on a simple click and on a mouse drag, it should use a system-supplied short distance threshold to decide between click and drag (users often perform inadvertent tiny movements during a click). It can be easily achieved by ignoring all drags with this method returning true and ignoring all clicks with this method returning false.
- Returns:
- true if there were no significant mouse movements (out of system hysteresis area) since the last pressed event that occurred before this event.
isShiftDown
public final boolean isShiftDown()
- Returns:
- true if the Shift modifier is down on this event
isControlDown
public final boolean isControlDown()
- Returns:
- true if the Control modifier is down on this event
isAltDown
public final boolean isAltDown()
- Returns:
- true if the Alt modifier is down on this event
isMetaDown
public final boolean isMetaDown()
- Returns:
- true if the Meta modifier is down on this event
isSynthesized
public boolean isSynthesized()
- Returns:
- true if this event is synthesized from using a touch screen
- Since:
- JavaFX 2.2
isShortcutDown
public final boolean isShortcutDown()
control on Windows and meta (command key) on Mac.- Returns:
-
trueif the shortcut modifier is down,falseotherwise
isPopupTrigger
public final boolean isPopupTrigger()
true if this mouse event is the popup menu trigger event for the platform. Note: Popup menus are triggered differently on different systems. Therefore, popupTrigger should be checked in both onMousePressed and mouseReleased for proper cross-platform functionality.
- Returns:
-
trueif this mouse event is the popup menu trigger event for the platform - Since:
- JavaFX 8.0
isPrimaryButtonDown
public final boolean isPrimaryButtonDown()
true if primary button (button 1, usually the left) is currently pressed. Note that this is different from the getButton() method that indicates which button press was responsible for this event while this method indicates whether the primary button is depressed.- Returns:
-
trueif primary button (button 1, usually the left) is currently pressed
isSecondaryButtonDown
public final boolean isSecondaryButtonDown()
true if secondary button (button 3, usually the right) is currently pressed. Note that this is different from the getButton() method that indicates which button press was responsible for this event while this method indicates whether the secondary button is depressed.- Returns:
-
trueif secondary button (button 3, usually the right) is currently pressed
isMiddleButtonDown
public final boolean isMiddleButtonDown()
true if middle button (button 2) is currently pressed. Note that this is different from the getButton() method that indicates which button press was responsible for this event while this method indicates whether the middle button is depressed.- Returns:
-
trueif middle button (button 2) is currently pressed
isBackButtonDown
public final boolean isBackButtonDown()
true if back button (button 4) is currently pressed. Note that this is different from the getButton() method that indicates which button press was responsible for this event while this method indicates whether the back button is depressed.- Returns:
-
trueif back button (button 4) is currently pressed - Since:
- 12
isForwardButtonDown
public final boolean isForwardButtonDown()
true if forward button (button 5) is currently pressed. Note that this is different from the getButton() method that indicates which button press was responsible for this event while this method indicates whether the back button is depressed.- Returns:
-
trueif forward button (button 5) is currently pressed - Since:
- 12
toString
public String toString()
MouseEvent object.- Overrides:
-
toStringin classEventObject - Returns:
- a string representation of this
MouseEventobject.
getPickResult
public final PickResult getPickResult()
- Returns:
- new PickResult object that contains information about the pick
- Since:
- JavaFX 8.0
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.