Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

How to drag and drop with Java 2, Part 2

Create a library of drag and drop-enabled Swing components

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 6

Figure 1. UML diagram of D&D library classes

The Drag classes

A drag-enabled component implements the DragComponent interface. It creates an instance of DragGestureAdapter and an instance of DragSourceAdapter.

The DragSourceAdapter implements the DragSourceListener interface and maintains a reference to a DragComponent object. When a drag is initiated, the DragSourceAdapter queries the DragComponent for the acceptable drag operation and an appropriate Transferable object. If this is a move operation, the DragSourceAdapter will tell the DragComponent to move the data. The move operation actually adds the data to the destination, then removes the data from the source at the end of the D&D operation. We'll discuss this more when we come to the JList and JTable examples. The drag-over effects are the DragSourceAdapter's responsibility. These are usually cursor changes. The DragComponent may register custom cursors with the DragSourceAdapter object.

The DragComponent uses a DragGestureAdapter object, which implements the DragGestureListener interface, in order to initiate the drag operation. With components such as a JTree, it is possible that not all nodes can be dragged. The DragGestureAdapter verifies the drag with the DragComponent's isStartDragOk() method, and it registers the DragComponent's DragSourceAdapter.

Figure 2. DragComponent message trace

The Drop classes

A drop-enabled component implements the DropComponent interface. It creates and maintains a reference to a DropTargetAdapter.

Figure 3. DropComponent message trace

Transferable models

For some components, we might not want to create an individual subclass for each type of data the component might represent. It would be better to create a transferable model for these components. When the drag operation starts, the component gets the Transferable from the model. This strategy works well for the JTable and JList.

JList

It is convenient to place all of the common D&D methods in an abstract subclass of JList. This abstract DnDList takes care of creating and registering the appropriate adapters, drag-under and drag-over feedback, autoscrolling, moving, and event/flavor validation. The concrete subclass specifies the acceptable operations and flavors, and defines an add() method that is called when the DropTargetAdapter receives the drop message.

We can define a TransferableListModel interface with method public Transferable getTransferable(int index). For convenience, we can define an AbstractDnDList model that has data structure maintenance code but requires that subclasses implement the getTransferable() method. This concrete subclass determines the exact type of Transferable returned. For example, a DnDTextList concrete subclass of DnDList would create a DnDTextListModel concrete subclass of AbstractDnDList; the latter in turn creates and returns a StringTransferable object from getTransferable.

Figure 4. D&D list model

JTable

The JTable follows the same pattern as the JList. We place the common D&D code in an abstract class that uses an abstract transferable model. The concrete table subclasses create a concrete transferable model.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources