The first set of methods we will examine are used for exporting data from a component. These methods are invoked for the drag gesture, or the cut/copy action, when the component in question is the source of the operation. TheTransferHandler
methods for exporting data are:
getSourceActions(JComponent)
— This method is used to query what actions are supported by the source component, such asCOPY
,MOVE
, orLINK
, in any combination. For example, a customer list might not support moving a customer name out of the list, but it would very likely support copying the customer name. Most of our examples support bothCOPY
andMOVE
.
createTransferable(JComponent)
— This method bundles up the data to be exported into aTransferable
object in preparation for the transfer.
exportDone(JComponent, Transferable, int)
— This method is invoked after the export is complete. When the action is aMOVE
, the data needs to be removed from the source after the transfer is complete — this method is where any necessary cleanup occurs.
Here are some sample implementations of the export methods:
int getSourceActions(JComponent c) { return COPY_OR_MOVE; } Transferable createTransferable(JComponent c) { return new StringSelection(c.getSelection()); } void exportDone(JComponent c, Transferable t, int action) { if (action == MOVE) { c.removeSelection(); } }Next we will look at the
TransferHandler
methods required for data import.