The Drop API

This example uses several of the classes found in the java.awt.dnd package. Here’s how they fit together (see Figure 24-3). Remember that we’re just looking at the drop side of DnD. We’ll explore the drag side in the next section and autoscrolling at the end of this chapter.

DnD class diagram for the drop side

Figure 24-3. DnD class diagram for the drop side

The DropTarget Class

An obvious starting point for playing with this API is the DropTarget class. This class encapsulates the functionality you add to regular GUI components so that they can respond to drop events. You can make just about anything a drop target. Well, not anything, but any Component can be a drop target. Typically, you pick the component that responds to the drop—such as the text area in our example—but you can also pick a proxy component that simply helps produce all the right events while your event handler plays with the dropped data.

Note that just because the DropTarget class implements the DropTargetListener interface, this does not mean that the target is already handling drop events for you. The DropTarget class implementation of the interface supports the internal mechanisms for responding to events; it isn’t used by us as programmers. We still have to create our own listener.


Table 24-4 lists five properties of the DropTarget class that govern the context of this target and its data transfer capabilities.

Table 24-4. DropTarget ...

Get Java Swing, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.