- java.lang.Object
-
- java.util.EventObject
-
- javafx.event.Event
-
- javafx.scene.input.InputEvent
-
- javafx.scene.input.DragEvent
-
- All Implemented Interfaces:
Serializable,Cloneable
public final class DragEvent extends InputEvent
Drag events replace mouse events during drag-and-drop gesture. The difference between press-drag-release and drag-and-drop gestures is described atMouseEvent.Drag and drop gesture can be started by calling
startDragAndDrop()(on a node or scene) inside of aDRAG_DETECTEDevent handler. The data to be transfered to drop target are placed to adragBoardat this moment.Drag entered/exited events behave similarly to mouse entered/exited events, please see
MouseEventoverview.Drag sources: initiating a drag and drop gesture
When a drag gesture is detected, an application can decide whether to start a drag and drop gesture or continue with a press-drag-release gesture.The default drag detection mechanism uses mouse movements with a pressed button in combination with hysteresis. This behavior can be augmented by the application. Each
MOUSE_PRESSEDandMOUSE_DRAGGEDevent has adragDetectflag that determines whether a drag gesture has been detected. The default value of this flag depends on the default detection mechanism and can be modified by callingsetDragDetect()inside of an event handler. When processing of one of these events ends with thedragDetectflag set to true, aDRAG_DETECTEDMouseEventis sent to the potential gesture source (the object on which a mouse button has been pressed). This event notifies about the gesture detection.Inside a
DRAG_DETECTEDevent handler, if thestartDragAndDrop()method is called on a node or scene and a dragged data is made available to the returnedDragboard, the object on whichstartDragAndDrop()has been called is considred a gesture source and the drag and drop gesture is started. TheDragboardhas system clipboard functionality but is specifically used for drag and drop data transfer.The
startDragAndDrop()method takes a set ofTransferModes supported by the gesture source. For instance passing onlyTransferMode.COPYindicates that the gesture source allows only copying of the data, not moving or referencing.Following example shows a simple drag and drop source:
Rectangle rect = new Rectangle(100, 100); rect.setOnDragDetected(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { Dragboard db = startDragAndDrop(TransferMode.ANY); ClipboardContent content = new ClipboardContent(); content.putString("Hello!"); db.setContent(content); event.consume(); } });Potential drop targets
After the drag and drop gesture has been started, any object (
Node,Scene) over which the mouse is dragged is a potential drop target.When the mouse is dragged into the boundaries of potential drop target, the potential target gets a
DRAG_ENTEREDevent. When the mouse is dragged outside of the potential target's bounds, it gets aDRAG_EXITEDevent. There are also the bubblingDRAG_ENTERED_TARGETandDRAG_EXITED_TARGETvariants. They behave similarly to mouse entered/exited events, please seeMouseEventoverview.A potential drop target can decide to change its appearance to let the user know that the dragged data can be dropped on it. This can be done in a
DRAG_OVERevent handler, based on the position of the mouse. Another option is to change the potential target's appearance in aDRAG_ENTEREDandDRAG_EXITEDhandlers.In
DRAG_OVERevent handler a potential drop target has the ability to make it known that it is an actual target. This is done by callingacceptTransferModes(TransferMode...)on the event, passing transfer modes it is willing to accept. If it is not called during the event delivery or if none of the passed transfer modes is supported by gesture source, then the potential drop target is not considered to be an actual drop target.When deciding weather to accept the event by calling
acceptTransferModes(TransferMode...), the type of data available on theDragboardshould be considered. Access to theDragboardis provided by thegetDragboard()method.When accepting an event, the potential gesture target decides which
TransferModeis accepted for the operation. To make the decision,DragBoard.getTransferModes()(set of transfer modes supported by the gesture source) andDragEvent.getTransferMode()(default transfer mode issued by platform, driven by key modifiers) can be used. It is poosible to pass more transfer modes into theacceptTransferModes(TransferMode...)method. In this case it makes the decision in behalf of the application (it chooses the default mode if it's supported by gesture source and accepted by gesture target, otherwise it chooses the most common mode of the supported and accepted ones). TheDRAG_DROPPEDevent'sgetTransferMode()later reports the transfer mode accepted by theDRAG_OVERevent handler.A drag and drop gesture ends when the mouse button is released. If this happens over a gesture target that accepted previous
DRAG_OVERevents with a transfer mode supported by gesture source, aDRAG_DROPPEDevent is sent to the gesture target. In its handler, the gesture target can access the data on the dragboard. After data has been transferred (or decided not to transfer), the gesture needs to be completed by callingsetDropCompleted(Boolean)on the event. TheBooleanargument indicates if the data has been transferred successfully or not. If it is not called, the gesture is considered unsuccessful.Following example shows a simple drag and drop target for text data:
Rectangle rect = new Rectangle(100, 100); rect.setOnDragOver(new EventHandler<DragEvent>() { @Override public void handle(DragEvent event) { Dragboard db = event.getDragboard(); if (db.hasString()) { event.acceptTransferModes(TransferMode.COPY_OR_MOVE); } event.consume(); } }); rect.setOnDragDropped(new EventHandler<DragEvent>() { @Override public void handle(DragEvent event) { Dragboard db = event.getDragboard(); boolean success = false; if (db.hasString()) { System.out.println("Dropped: " + db.getString()); success = true; } event.setDropCompleted(success); event.consume(); } });Drag sources: finalizing drag and drop gesture
After the gesture has been finished, whether by successful or unsuccessful data transfer or being canceled, the
DRAG_DONEevent is sent to the gesture source. ThegetTransferMode()method of the event indicates to the gesture source how the transfer of data was completed. If the transfer mode has the valueMOVE, then this allows the source to clear out its data. Clearing the source's data gives the appropriate appearance to a user that the data has been moved by the drag and drop gesture. If it has the valuenull, then the drag and drop gesture ended without any data being transferred. This could happen as a result of a mouse release event over a node that is not a drop target, or the user pressing the ESC key to cancel the drag and drop gesture, or by the gesture target reporting an unsuccessful data transfer.- Since:
- JavaFX 2.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static EventType<DragEvent>ANYCommon supertype for all drag event types.static EventType<DragEvent>DRAG_DONEThis event occurs on drag-and-drop gesture source after its data has been dropped on a drop target.static EventType<DragEvent>DRAG_DROPPEDThis event occurs when the mouse button is released during drag and drop gesture on a drop target.static EventType<DragEvent>DRAG_ENTEREDThis event occurs when drag gesture enters a node.static EventType<DragEvent>DRAG_ENTERED_TARGETThis event occurs when drag gesture enters a node.static EventType<DragEvent>DRAG_EXITEDThis event occurs when drag gesture exits a node.static EventType<DragEvent>DRAG_EXITED_TARGETThis event occurs when drag gesture exits a node.static EventType<DragEvent>DRAG_OVERThis event occurs when drag gesture progresses within this node.-
Fields inherited from class javafx.event.Event
consumed, eventType, NULL_SOURCE_TARGET, target
-
Fields inherited from class java.util.EventObject
source
-
-
Constructor Summary
Constructors Constructor Description DragEvent(Object source, EventTarget target, EventType<DragEvent> eventType, Dragboard dragboard, double x, double y, double screenX, double screenY, TransferMode transferMode, Object gestureSource, Object gestureTarget, PickResult pickResult)Constructs new DragEvent event.DragEvent(EventType<DragEvent> eventType, Dragboard dragboard, double x, double y, double screenX, double screenY, TransferMode transferMode, Object gestureSource, Object gestureTarget, PickResult pickResult)Constructs new DragEvent event with empty source and target.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidacceptTransferModes(TransferMode... transferModes)Accepts thisDragEvent, choosing the transfer mode for the drop operation.DragEventcopyFor(Object newSource, EventTarget newTarget)Creates and returns a copy of this event with the specified event source and target.DragEventcopyFor(Object source, EventTarget target, Object gestureSource, Object gestureTarget, EventType<DragEvent> eventType)Creates a copy of the given drag event with the given fields substituted.DragEventcopyFor(Object source, EventTarget target, EventType<DragEvent> type)Creates a copy of the given drag event with the given fields substituted.TransferModegetAcceptedTransferMode()Gets transfer mode accepted by potential target.ObjectgetAcceptingObject()The object that accepted the drag.DragboardgetDragboard()A dragboard that is available to transfer data.EventType<DragEvent>getEventType()Gets the event type of this event.ObjectgetGestureSource()The source object of the drag and drop gesture.ObjectgetGestureTarget()The target object of the drag and drop gesture.PickResultgetPickResult()Returns information about the pick.doublegetSceneX()Returns horizontal position of the event relative to the origin of theScenethat contains the DragEvent's source.doublegetSceneY()Returns vertical position of the event relative to the origin of theScenethat contains the DragEvent's source.doublegetScreenX()Returns absolute horizontal position of the event.doublegetScreenY()Returns absolute vertical position of the event.TransferModegetTransferMode()Data transfer mode.doublegetX()Horizontal position of the event relative to the origin of the DragEvent's source.doublegetY()Vertical position of the event relative to the origin of the DragEvent's source.doublegetZ()Depth position of the event relative to the origin of the MouseEvent's source.booleanisAccepted()Indicates if this event has been accepted.booleanisDropCompleted()WhethersetDropCompleted(true)has been called on this event.voidsetDropCompleted(boolean isTransferDone)Indicates that transfer handling of thisDragEventwas completed successfully during aDRAG_DROPPEDevent handler.-
Methods inherited from class java.util.EventObject
getSource, toString
-
-
-
-
Field Detail
-
DRAG_ENTERED_TARGET
public static final EventType<DragEvent> DRAG_ENTERED_TARGET
This event occurs when drag gesture enters a node. It's the bubbling variant, which is delivered also to all parents of the entered node (unless it was consumed). When notifications about entering some of node's children are not desired,DRAG_ENTEREDevent handler should be used.
-
DRAG_ENTERED
public static final EventType<DragEvent> DRAG_ENTERED
This event occurs when drag gesture enters a node. This event type is delivered only to the entered node, if parents want to filter it or get the bubbling event, they need to useDRAG_ENTERED_TARGET.
-
DRAG_EXITED_TARGET
public static final EventType<DragEvent> DRAG_EXITED_TARGET
This event occurs when drag gesture exits a node. It's the bubbling variant, which is delivered also to all parents of the eixited node (unless it was consumed). When notifications about exiting some of node's children are not desired,DRAG_EXITEDevent handler should be used.
-
DRAG_EXITED
public static final EventType<DragEvent> DRAG_EXITED
This event occurs when drag gesture exits a node. This event type is delivered only to the exited node, if parents want to filter it or get the bubbling event, they need to useDRAG_EXITED_TARGET.
-
DRAG_OVER
public static final EventType<DragEvent> DRAG_OVER
This event occurs when drag gesture progresses within this node.
-
DRAG_DONE
public static final EventType<DragEvent> DRAG_DONE
This event occurs on drag-and-drop gesture source after its data has been dropped on a drop target. ThetransferModeof the event shows what just happened at the drop target. IftransferModehas the valueMOVE, then the source can clear out its data. Clearing the source's data gives the appropriate appearance to a user that the data has been moved by the drag and drop gesture. AtransferModethat has the valueNONEindicates that no data was transferred during the drag and drop gesture.
-
-
Constructor Detail
-
DragEvent
public DragEvent(Object source, EventTarget target, EventType<DragEvent> eventType, Dragboard dragboard, double x, double y, double screenX, double screenY, TransferMode transferMode, Object gestureSource, Object gestureTarget, PickResult pickResult)
Constructs new DragEvent event. For DRAG_DROPPED and DRAG_DONE event types, theacceptedstate andacceptedTransferModeare set according to the passedtransferMode.- 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.dragboard- the dragboard 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.transferMode- the transfer mode of the event.gestureSource- the source of the DnD gesture of the event.gestureTarget- the target of the DnD gesture of the event.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 the target- Since:
- JavaFX 8.0
-
DragEvent
public DragEvent(EventType<DragEvent> eventType, Dragboard dragboard, double x, double y, double screenX, double screenY, TransferMode transferMode, Object gestureSource, Object gestureTarget, PickResult pickResult)
Constructs new DragEvent event with empty source and target.- Parameters:
eventType- The type of the event.dragboard- the dragboard 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.transferMode- the transfer mode of the event.gestureSource- the source of the DnD gesture of the event.gestureTarget- the target of the DnD gesture of the event.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
-
-
Method Detail
-
copyFor
public DragEvent copyFor(Object source, EventTarget target, Object gestureSource, Object gestureTarget, EventType<DragEvent> eventType)
Creates a copy of the given drag event with the given fields substituted.- Parameters:
source- the new source of the copied eventtarget- the new target of the copied eventgestureSource- the new gesture source.gestureTarget- the new gesture target.eventType- the new eventType- Returns:
- the event copy with the fields
- Since:
- JavaFX 8.0
-
copyFor
public DragEvent copyFor(Object newSource, EventTarget newTarget)
Description copied from class:EventCreates and returns a copy of this event with the specified event source and target. If the source or target is set tonull, it is replaced by theNULL_SOURCE_TARGETvalue.
-
copyFor
public DragEvent copyFor(Object source, EventTarget target, EventType<DragEvent> type)
Creates a copy of the given drag event with the given fields substituted.- Parameters:
source- source of the copied eventtarget- target of the copied eventtype- type of event- Returns:
- the event copy with the fields
- Since:
- JavaFX 8.0
-
getEventType
public EventType<DragEvent> getEventType()
Description copied from class:EventGets the event type of this event. Objects of the sameEventclass can have different event types. These event types further specify what kind of event occurred.- Overrides:
getEventTypein classInputEvent- Returns:
- the event type
-
getX
public final double getX()
Horizontal position of the event relative to the origin of the DragEvent's source.- Returns:
- horizontal position of the event relative to the origin of the DragEvent's source
-
getY
public final double getY()
Vertical position of the event relative to the origin of the DragEvent's source.- Returns:
- vertical position of the event relative to the origin of the DragEvent's source
-
getZ
public final double getZ()
Depth position of the event relative to the origin of the MouseEvent's source.- 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.- Returns:
- absolute horizontal position of the event
-
getScreenY
public final double getScreenY()
Returns absolute vertical position of the event.- Returns:
- absolute vertical position of the event
-
getSceneX
public final double getSceneX()
Returns horizontal position of the event relative to the origin of theScenethat contains the DragEvent's source. If the node is not in aScene, then the value is relative to the boundsInParent of the root-most parent of the DragEvent'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 DragEvent's source
-
getSceneY
public final double getSceneY()
Returns vertical position of the event relative to the origin of theScenethat contains the DragEvent's source. If the node is not in aScene, then the value is relative to the boundsInParent of the root-most parent of the DragEvent'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 DragEvent's source
-
getPickResult
public final PickResult getPickResult()
Returns information about the pick.- Returns:
- new PickResult object that contains information about the pick
- Since:
- JavaFX 8.0
-
getGestureSource
public final Object getGestureSource()
The source object of the drag and drop gesture. Gesture source is the object that started drag and drop operation. The valuenullis valid in the case that the gesture comes from another application.- Returns:
- the source object of the drag and drop gesture
-
getGestureTarget
public final Object getGestureTarget()
The target object of the drag and drop gesture. Gesture target is the object that accepts drag events. The valuenullis valid in the case that the drag and drop gesture has been canceled or completed without a transfer taking place or there is currently no event target accepting the drag events.- Returns:
- the target object of the drag and drop gesture
-
getTransferMode
public final TransferMode getTransferMode()
Data transfer mode. Before the data transfer is is performed, this is the default transfer mode set by system according to input events such as the user holding some modifiers. In time of data transfer (in DRAG_DROPPED event) it determines the transfer mode accepted by previous DRAG_OVER handler. After the data transfer (in DRAG_DONE event) it determines the actual mode of the transfer done.- Returns:
- the data transfer mode
-
isAccepted
public final boolean isAccepted()
Indicates if this event has been accepted.- Default value:
- false
- Returns:
- is this event has been accepted
- See Also:
acceptTransferModes(javafx.scene.input.TransferMode...)
-
getAcceptedTransferMode
public final TransferMode getAcceptedTransferMode()
Gets transfer mode accepted by potential target.- Returns:
- transfer mode accepted by potential target
-
getAcceptingObject
public final Object getAcceptingObject()
The object that accepted the drag.- Returns:
- the object that accepted the drag
- Since:
- JavaFX 8.0
-
getDragboard
public final Dragboard getDragboard()
A dragboard that is available to transfer data. Data can be placed onto this dragboard in handler of theDRAG_DETECTEDmouse event. Data can be copied from this dragboard in handler of theDRAG_DROPPEDevent.- Returns:
- a dragboard that is available to transfer data
-
acceptTransferModes
public void acceptTransferModes(TransferMode... transferModes)
Accepts thisDragEvent, choosing the transfer mode for the drop operation. Used to indicate that the potential drop target that receives this event is a drop target fromDRAG_OVERevent handler.It accepts one of the transfer modes that are both passed into this method and supported by the gesture source. It accepts the default transfer mode if possible, otherwise the most common one of the acceptable modes.
- Parameters:
transferModes- the transfer mode for the drop operation.
-
setDropCompleted
public void setDropCompleted(boolean isTransferDone)
Indicates that transfer handling of thisDragEventwas completed successfully during aDRAG_DROPPEDevent handler. Nodragboardaccess can happen after this call.- Parameters:
isTransferDone-trueindicates that the transfer was successful.- Throws:
IllegalStateException- if this is not a DRAG_DROPPED event
-
isDropCompleted
public boolean isDropCompleted()
WhethersetDropCompleted(true)has been called on this event.- Returns:
- true if
setDropCompleted(true)has been called
-
-