Top Description Inners Fields Constructors Methods
javax.swing

public Class TransferHandler

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
javax.swing.plaf.basic.BasicColorChooserUI.ColorTransferHandler, javax.swing.plaf.basic.BasicListUI.ListTransferHandler, javax.swing.plaf.basic.BasicTableUI.TableTransferHandler, javax.swing.plaf.basic.BasicTextUI.TextTransferHandler, javax.swing.plaf.basic.BasicTreeUI.TreeTransferHandler, javax.swing.plaf.basic.BasicFileChooserUI.FileTransferHandler, javax.swing.text.JTextComponent.DefaultTransferHandler
Annotations
@SuppressWarnings:serial
Imports
java.awt.*, java.awt.event.*, java.awt.datatransfer.*, java.awt.dnd.*, java.beans.*, java.lang.reflect.*, java.io.*, java.util.TooManyListenersException, javax.swing.plaf.UIResource, javax.swing.event.*, javax.swing.text.JTextComponent, sun.reflect.misc.MethodUtil, sun.swing.SwingUtilities2, sun.swing.*, sun.awt.AppContext, .SunToolkit, .AWTAccessor, java.security.AccessController, .PrivilegedAction, .AccessControlContext, jdk.internal.access.SharedSecrets, .JavaSecurityAccess

This class is used to handle the transfer of a Transferable to and from Swing components. The Transferable is used to represent data that is exchanged via a cut, copy, or paste to/from a clipboard. It is also used in drag-and-drop operations to represent a drag from a component, and a drop to a component. Swing provides functionality that automatically supports cut, copy, and paste keyboard bindings that use the functionality provided by an implementation of this class. Swing also provides functionality that automatically supports drag and drop that uses the functionality provided by an implementation of this class. The Swing developer can concentrate on specifying the semantics of a transfer primarily by setting the transferHandler property on a Swing component.

This class is implemented to provide a default behavior of transferring a component property simply by specifying the name of the property in the constructor. For example, to transfer the foreground color from one component to another either via the clipboard or a drag and drop operation a TransferHandler can be constructed with the string "foreground". The built in support will use the color returned by getForeground as the source of the transfer, and setForeground for the target of a transfer.

Please see How to Use Drag and Drop and Data Transfer, a section in The Java Tutorial, for more information.

Authors
Timothy Prinzing, Shannon Hickey
Since
1.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
TransferHandler.DragHandler

This is the default drag handler for drag and drop operations that use the TransferHandler.

private static class
public static class
TransferHandler.DropLocation

Represents a location where dropped data should be inserted.

pack-priv static interface
TransferHandler.HasGetTransferHandler

An interface to tag things with a getTransferHandler method.

pack-priv static class
private static class
pack-priv static class
TransferHandler.SwingDropTarget

This is the default drop target for drag and drop operations if one isn't provided by the developer.

pack-priv static class
public static class
TransferHandler.TransferSupport

This class encapsulates all relevant details of a clipboard or drag and drop transfer, and also allows for customizing aspects of the drag and drop experience.

Field Summary

Modifier and TypeField and Description
public static final int
COPY

An int representing a "copy" transfer action.

public static final int
COPY_OR_MOVE

An int representing a source action capability of either "copy" or "move".

pack-priv static final Action
pack-priv static final Action
private Image
dragImage

image for the startDrag method

private Point
dragImageOffset

anchor offset for the startDrag method

public static final int
LINK

An int representing a "link" transfer action.

public static final int
MOVE

An int representing a "move" transfer action.

public static final int
NONE

An int representing no transfer action.

pack-priv static final Action
private String
private static TransferHandler.SwingDragGestureRecognizer

Constructor Summary

AccessConstructor and Description
public
TransferHandler(String
the name of the property to transfer; this can be null if there is no property associated with the transfer handler (a subclass that performs some other kind of transfer, for example)
property
)

Constructs a transfer handler that can transfer a Java Bean property from one component to another via the clipboard or a drag and drop operation.

protected
TransferHandler()

Convenience constructor for subclasses.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the import can happen, false otherwise
canImport
(TransferHandler.TransferSupport
the object containing the details of the transfer, not null.
support
)

This method is called repeatedly during a drag and drop operation to allow the developer to configure properties of, and to return the acceptability of transfers; with a return value of true indicating that the transfer represented by the given TransferSupport (which contains all of the details of the transfer) is acceptable at the current time, and a value of false rejecting the transfer.

public boolean

Returns:

true if the data can be inserted into the component, false otherwise
canImport
(JComponent
the component to receive the transfer; provided to enable sharing of TransferHandlers
comp
,
DataFlavor[]
the data formats available
transferFlavors
)

Indicates whether a component will accept an import of the given set of data flavors prior to actually attempting to import it.

protected Transferable

Returns:

the representation of the data to be transferred, or null if the property associated with c is null
createTransferable
(JComponent
the component holding the data to be transferred; provided to enable sharing of TransferHandlers
c
)

Creates a Transferable to use as the source for a data transfer.

public void
exportAsDrag(JComponent
the component holding the data to be transferred; provided to enable sharing of TransferHandlers
comp
,
InputEvent
the event that triggered the transfer
e
,
int
the transfer action initially requested; either COPY, MOVE or LINK; the DnD system may change the action used during the course of the drag operation
action
)

Causes the Swing drag support to be initiated.

protected void
exportDone(JComponent
the component that was the source of the data
source
,
Transferable
The data that was transferred or possibly null if the action is NONE.
data
,
int
the actual action that was performed
action
)

Invoked after data has been exported.

public void
exportToClipboard(JComponent
the component holding the data to be transferred; provided to enable sharing of TransferHandlers
comp
,
Clipboard
the clipboard to transfer the data into
clip
,
int
the transfer action requested; this should be a value of either COPY or MOVE; the operation performed is the intersection of the transfer capabilities given by getSourceActions and the requested action; the intersection may result in an action of NONE if the requested action isn't supported
action
)

Causes a transfer from the given component to the given clipboard.

public static Action

Returns:

an Action for performing copies to the clipboard
getCopyAction
()

Returns an Action that performs copy operations to the clipboard.

public static Action

Returns:

an Action for performing cuts to the clipboard
getCutAction
()

Returns an Action that performs cut operations to the clipboard.

public Image

Returns:

the reference to the drag image
getDragImage
()

Returns the drag image.

public Point

Returns:

a Point object that corresponds to coordinates of an anchor offset of the image relative to the upper left corner of the image. The point (0,0) returns by default.
getDragImageOffset
()

Returns an anchor offset for the image to drag.

private static DropTargetListener
public static Action

Returns:

an Action for performing pastes from the clipboard
getPasteAction
()

Returns an Action that performs paste operations from the clipboard.

private DataFlavor
getPropertyDataFlavor(Class<?> k, DataFlavor[] flavors)

Fetches the data flavor from the array of possible flavors that has data of the type represented by property type.

private PropertyDescriptor
getPropertyDescriptor(JComponent comp)

Fetches the property descriptor for the property assigned to this transfer handler on the given component (transfer handler may be shared).

public int

Returns:

COPY if the transfer property can be found, otherwise returns NONE
getSourceActions
(JComponent
the component holding the data to be transferred; provided to enable sharing of TransferHandlers
c
)

Returns the type of transfer actions supported by the source; any bitwise-OR combination of COPY, MOVE and LINK.

public Icon

Returns:

null, indicating there is no default visual representation
getVisualRepresentation
(Transferable
the data to be transferred; this value is expected to have been created by the createTransferable method
t
)

Returns an object that establishes the look of a transfer.

public boolean

Returns:

true if the data was inserted into the component, false otherwise
importData
(TransferHandler.TransferSupport
the object containing the details of the transfer, not null.
support
)

Causes a transfer to occur from a clipboard or a drag and drop operation.

public boolean

Returns:

true if the data was inserted into the component, false otherwise
importData
(JComponent
the component to receive the transfer; provided to enable sharing of TransferHandlers
comp
,
Transferable
the data to import
t
)

Causes a transfer to a component from a clipboard or a DND drop operation.

public void
setDragImage(Image
an image to drag
img
)

Sets the drag image parameter.

public void
setDragImageOffset(Point
a Point object that corresponds to coordinates of an anchor offset of the image relative to the upper left corner of the image
p
)

Sets an anchor offset for the image to drag.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait