At the heart of the data transfer mechanism is the TransferHandler class. As its name suggests, the
TransferHandler provides an easy mechanism for transferring data to and from a
JComponent — all the details are contained in this class and its supporting classes. Most components are provided with a default transfer handler. You can create and install your own transfer handler on any component.
There are three methods used to engage a
TransferHandler on a component:
setDragEnabled(boolean) — turns on drag support. (The default is false.) This method is defined on each component that supports the drag gesture; the link takes you to the documentation for
setDropMode(DropMode) — configures how drop locations are determined. This method is defined for
JTree; the link takes you to the documentation for
setTransferHandler(TransferHandler) — used to plug in custom data import and export. This method is defined on
JComponent, so it is inherited by every Swing component.
As mentioned previously, the default Swing transfer handlers, such as those used by text components and the color chooser, provide the support considered to be most useful for both importing and exporting of data. However list, table, and tree do not support drop by default. The reason for this is that there is no all-purpose way to handle a drop on these components. For example, what does it mean to drop on a particular node of a
JTree ? Does it replace the node, insert below it, or insert as a child of that node? Also, we do not know what type of model is behind the tree — it might not be mutable.
While Swing cannot provide a default implementation for these components, the framework for drop is there. You need only to provide a custom
TransferHandler that manages the actual import of data.
If you install a custom
TransferHandler onto a Swing component, the default support is replaced. For example, if you replace
TransferHandler with one that handles colors only, you will disable its ability to support import and export of text.
If you must replace a default
TransferHandler — for example, one that handles text — you will need to re-implement the text import and export ability. This does not need to be as extensive as what Swing provides — it could be as simple as supporting the
StringFlavor data flavor, depending on your application's needs.
Next we show what
TransferHandler methods are required to implement data export.