Java Drag and Drop Example

Drag and Drop Applet

Drag one item at a time between List1 and List2
NOTE: these lists implement both DragSourceListener and DragTargetListener interfaces.
When a drag is started, as long as the cursor is over the list that started the drag,
that list sees the drag event in both interfaces, as a Source and a Target (a list can drop onto itself).
NOTE: Need Java 6 (or later version).

Applet failed to run. No Java plug-in was found

Source Code

DnDList.java (a JList that is both a drop Target and a drop Source, along with a JTextArea for observing the methods being called).
TestDnD.java (the code for displaying two two lists for dropping and dragging)

Any Component can have a DragSource associated with it via a DragGestureRecognizer.
A DragGestureRecognizer associates all the various drag initiation gestures with a list of DragGestureListeners .

Some notes/observations about Java Drag and Drop API

DragSource has two useful methods and only an empty constructor

createDefaultDragGestureRecognizer(Component c, int actions, DragGestureListener dgl)
A default DragGestureRecognizer is created and it will call DragGestureListener dgl when a drag action is initiated over Component c
(types of drag actions can be found in DndConstants)
startDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl)
Start a drag given trigger event DragGestureEvent trigger, use a cursor, defined in DragSource of type Cursor dragCursor to transer data
Transferable transferable, monitor the drag with DragSourceListener dsl
When creatting a Transferable a data flavor (data type) is associated with the data, the drop target uses this data flavor to decide if the drop is acceptable.

DragGestureListener has 1 method

dragGestureRecognized(DragGestureEvent dge)
Called when a drag gesture has started, setup and call the dragSource.startDrag method here

DragSourceListener has 5 methods

dragEnter(DragSourceDragEvent event)
cursor enters a drop site
dragExit(DragSourceEvent event)
cursor exits a drop site
dragOver(DragSourceDragEvent event)
cursor over a drop site
dropActionChanged(DragSourceDragEvent event)
user changed the drop action (perhaps from a move to a copy)
dragDropEnd(DragSourceDropEvent event)
The drop is complete (status of the drop can be checked here)

DropTarget

DropTarget(Component c, DropTargetListener dtl)
Constructor that associated this DropTarget with this Component c and listens for drop events with DropTargetListener dtl

DropTargetListener has 5 methods

dragEnter(DropTargetDragEvent event)
cursor has entered this drop site
dragExit(DropTargetEvent event)
cursor has exited this drop site
dragOver(DropTargetDragEvent event)
cursor is over this drop site
dropActionChanged(DropTargetDragEvent event)
user changed drop type while over this drop site (perhaps from a copy to a move)
drop(DropTargetDropEvent event)
drop has occured over this site
Use the DropTargetDropEvent event to see if the transfer data is of an acceptable data flavor.
Either reject or accept the drop, event.rejectDrop() or event.acceptDrop(int dropAction) (dropAction can be found in DnDConstants)