7.14.3. Drop Targets

In this section, we’ll show you how to implement a drop target. Our example is again a JList with image icons. We’ll add drop support so that users can drop images into the list.

To make a component into a drop target, set a TransferHandler and implement the canImport and importData methods.

Image Note

You can add a transfer handler to a JFrame. This is most commonly used for dropping files into an application. Valid drop locations include the frame decorations and the menu bar, but not components contained in the frame (which have their own transfer handlers).

The canImport method is called continuously as the user moves the ...

Get Core Java® Volume II—Advanced Features, Ninth Edition now with O’Reilly online learning.

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