6.2. Cut-and-Paste

In addition to the data transfer framework classes, the
java.awt.datatransfer package also defines
the Clipboard class and
the ClipboardOwner interface, which
implement data transfer with the cut-and-paste
metaphor.

A typical cut-and-paste transfer works as follows:

In the initiating application, the user types
Ctrl-C or
Ctrl-X or in some other way
tells the application that he
wants to copy or cut some data.

The application takes the selected data
and encapsulates it in an appropriate
Transferable object. The next step is
to call the
getSystemClipboard() method of the
Toolkit object, to get a
Clipboard object. The
application then calls the setContents()
method of the Clipboard, passing the
Transferable object as the new clipboard
contents.

If the user issued a cut command, the initiating
application typically deletes the data after transferring
it to the clipboard. If the user issued a copy command, however,
the application typically just highlights the data to
make it clear to the user what data is available
for pasting. Often this data should remain highlighted for
as long as the initiating application owns the clipboard.
When an application calls setContents(),
it becomes the clipboard owner and remains such
until some other application transfers data to the
clipboard. The application must, in fact, pass a object
that implements ClipboardOwner to the
setContents() method. This object is
used to notify the application when it ceases to be the
clipboard owner. Until that happens, however, the
application must maintain the
Transferable object and be willing to
provide the data when it is requested.

At some point, the user moves his attention to some other
application and issues a paste command in that
application. This receiving first application calls
Toolkit.getSystemClipboard() to obtain a
Clipboard object. Then it calls
getContents() to obtain a
Transferable object that represents the
data available on the clipboard. The application uses
getTransferDataFlavors() or
isDataFlavorSupported() to see if the
clipboard data is available in a format it is willing
to accept. If there is such a format, the application
calls getTransferData() to
transfer the data.

At some point after this cut-and-paste operation, the user
cuts or copies
a new piece of data in an application. At this point,
the original application ceases to be the clipboard owner
and no longer has to make its data available for pasting.
The lostOwnership() method of the
originating application's ClipboardOwner
object is called to notify the application of this occurrence.