B4J Programming Press on the image to return to the main documentation page.

jDragAndDrop

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.

List of types:

DragAndDrop
DragEvent

DragAndDrop

The DragAndDrop object lets you designate individual Nodes in your layout to be a drag source or a drop target
or indeed both if required.

Events:

None

Members:


  MakeDragSource (source As javafx.scene.Node, eventname As String)

  MakeDragTarget (target As javafx.scene.Node, eventname As String)

  SetDragModeAndData (transfermode As javafx.scene.input.TransferMode, dataid As String, dataobject As Object)

  Version As Double [read only]

Members description:

MakeDragSource (source As javafx.scene.Node, eventname As String)
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.
MakeDragTarget (target As javafx.scene.Node, eventname As String)
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.
SetDragModeAndData (transfermode As javafx.scene.input.TransferMode, dataid As String, dataobject As Object)
Set the drag and drop operation parameters.

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.

DataId is an arbitrary string identifying the type of content being dragged and dropped.
In order to be case insensitive this value will be automatically upper-cased in the library.

DataObject is the object that will be dragged and dropped.
Version As Double [read only]
Returns the version number of the library.

DragEvent

A DragEvent object is passed to all the drag and drop events except DragDetected which receives a MouseEvent.

In the source DragDone event handler, get the transfer mode by calling the GetTransferMode method on the event.
If the transfer mode is Null then that means the data transfer did not happen

Remember to call Consume if you don't want the event to be passed on to the parent Node.

Events:

None

Members:


  AcceptTransferMode (mode As javafx.scene.input.TransferMode)

  Consume

  GetDataId As String

  GetDataObject As Object

  GetGestureSource As Object

  GetGestureTarget As Object

  GetTransferMode As javafx.scene.input.TransferMode

  IsInitialized As Boolean

  SetDropCompleted (istransferdone As Boolean)

  X As Double [read only]

  Y As Double [read only]

Members description:

AcceptTransferMode (mode As javafx.scene.input.TransferMode)
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.
Consume
GetDataId As String
Gets the DataID of the object being dragged and dropped.
GetDataObject As Object
Returns the object being dragged and dropped.
GetGestureSource As Object
Returns the source object of the drag and drop gesture.
GetGestureTarget As Object
Returns the target object of the drag and drop gesture.
GetTransferMode As javafx.scene.input.TransferMode
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.
IsInitialized As Boolean
SetDropCompleted (istransferdone As Boolean)
Indicates whether transfer handling of this DragEvent was completed successfully or not during a DragDropped event.
X As Double [read only]
Gets the horizontal position of the event relative to the origin of the DragEvent's source.
Y As Double [read only]
Gets the vertical position of the event relative to the origin of the DragEvent's source.
Top