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).
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
DragGestureListener
s .
DragSource
has two useful methods and only an empty constructorcreateDefaultDragGestureRecognizer(Component c, int actions, DragGestureListener dgl)
DragGestureRecognizer
is created and it will call
DragGestureListener dgl
when a drag action
is initiated over
Component c
DndConstants
)
startDrag(DragGestureEvent trigger, Cursor dragCursor, Transferable transferable, DragSourceListener dsl)
DragGestureEvent trigger
, use a cursor, defined in
DragSource
of type Cursor dragCursor
to transer data
Transferable transferable
, monitor the drag with DragSourceListener dsl
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 methoddragGestureRecognized(DragGestureEvent dge)
dragSource.startDrag
method here
DragSourceListener
has 5 methodsdragEnter(DragSourceDragEvent event)
dragExit(DragSourceEvent event)
dragOver(DragSourceDragEvent event)
dropActionChanged(DragSourceDragEvent event)
dragDropEnd(DragSourceDropEvent event)
DropTarget
DropTarget(Component c, DropTargetListener dtl)
DropTarget
with this Component c
and listens
for drop events with DropTargetListener dtl
DropTargetListener
has 5 methods
dragEnter(DropTargetDragEvent event)
dragExit(DropTargetEvent event)
dragOver(DropTargetDragEvent event)
dropActionChanged(DropTargetDragEvent event)
drop(DropTargetDropEvent event)
DropTargetDropEvent event
to see if the transfer data is of an acceptable
data flavor.
event.rejectDrop()
or
event.acceptDrop(int dropAction)
(dropAction can be found in DnDConstants
)