Press on the image to return to the main documentation page.
Written by Andrew Graham
A drag-and-drop operation is a data transfer between two objects: a gesture source and a gesture target. The gesture source and gesture target can belong to a single JavaFX application or to two different JavaFX applications. If you run two copies of the library demo you will see that you can drag and drop between them
A drag-and-drop gesture happens as follows: The user clicks a mouse button on a gesture source, drags the mouse, and releases the mouse button on a gesture target. While dragging the data, the user gets visual feedback, which denotes locations that do not accept the data and, when over a target that accepts the data, the user is given a hint where to drop the data.
During the drag-and-drop gesture, various types of data can be transferred such as text, images, URLs, files, bytes, and strings.
Transfer modes define the type of transfer that happens between the gesture source and gesture target. the available transfer modes are COPY, MOVE, and LINK.
A gesture source reports a transfer mode. A gesture target accepts a transfer mode.
The JavaFX drag and drop implementation allows for arbitrarily complicated scenarios with multiple simultaneous data types being dragged and dropped and is appropriately complex. To keep things simple and easy to manage in B4J code this exposure is deliberately restricted to allow only one object to be dragged and dropped at any one time.
Makes the specified node a drag source by adding the DragDetected and DragDone events to it. The DragDetected event Sub receives a MouseEvent object. A DragEvent object is passed to the DragDone event. The DragDetected event Sub should call SetDragModeAndData if it wants to start a drag and drop operation.
DragId is a string identifying the type of data being dragged. DragObject is the data to be transferred which must be a serializable object. TransferMode can be one of COPY Indicates copying of data is supported or intended. LINK Indicates linking of data is supported or intended. MOVE Indicates moving of data is supported or intended.
Once the drag and drop operation is complete the DragDone event is is sent to the gesture source to inform the source about how the gesture finished. In the DragDone event handler, obtain the transfer mode by calling the GetTransferMode method on the event. If the transfer mode is NULL that means the data transfer did not happen. If the mode is MOVE, then clear the data on the gesture source.
Makes the specified node a drag target by adding the DragEntered, DragExited, DragOver and DragDrop events to it.
After the drag-and-drop gesture is started, any node or scene that the mouse is dragged over is a potential target to drop the data. You specify which object accepts the data by implementing the DragOver event handler.
For a successful drag-and-drop operation, you must implement the DragOver event handler, which calls the AcceptTransferModes(TransferMode) method of the event, passing the transfer modes that the target intends to accept. If none of the passed transfer modes are supported by the gesture source, the potential target does not fit the given drag-and-drop gesture.
When the mouse button is released on the gesture target, which accepted previous DragOver events with a transfer mode supported by the gesture source, then the DragDropped event is sent to the gesture target.
In the DragDropped event handler, you must complete the drag-and-drop gesture by calling the SetDropCompleted(Boolean) method on the event otherwise the gesture is considered unsuccessful.
When the drag gesture enters the boundaries of a potential gesture target, the target receives a DragEntered event. When the drag gesture leaves the potential target�s boundaries, the target receives a DragExited event. You can use the DragEntered and DragExited event handlers to change the target�s appearance in order to provide visual feedback to the user. You should verify the DataId of the object and only change appearance if it is an appropriate DataId.
In the target DragOver event call the AcceptTransferMode method passing the transfer modes that the target intends to accept. Note that the type of data available on the must be taken into account when deciding whether to accept the event.
Gets the DataID of the object being dragged and dropped.
Returns the object being dragged and dropped.
Returns the source object of the drag and drop gesture.
Returns the target object of the drag and drop gesture.
Gets the data transfer mode. In the DragDone event handler if the transfer mode is NULL then that means the data transfer did not happen.
Indicates whether transfer handling of this DragEvent was completed successfully or not during a DragDropped event.
XAsDouble [read only]
Gets the horizontal position of the event relative to the origin of the DragEvent's source.
YAsDouble [read only]
Gets the vertical position of the event relative to the origin of the DragEvent's source.