Java Reference
In-Depth Information
The data-transfer architecture relies on object serialization as one of its means of
transferring data between applications, which makes the data-transfer architecture
quite general and flexible. It was designed to allow arbitrary data to be transferred
between independent Java virtual machines and even between Java applications
and native-platform applications. Unfortunately, the data transfer architecture has
apparently never been fully implemented, so data transfer between JVMs and
between a JVM and a native application only works if you use the special prede-
fined flavors, DataFlavor.stringFlavor and DataFlavor.javaFileListFlavor .
Although you can define other DataFlavor objects to represent other serializable
Java classes, these custom flavors work to transfer data only within a single JVM.
Simple Copy-and-Paste
While DataFlavor and Transferable provide the underlying infrastructure for data
transfer, it is the java.awt.datatransfer.Clipboard class and java.awt.data-
transfer.ClipboardOwner interface that support the cut-and-paste style of data
transfer. A typical cut-and-paste scenario works like this:
When the user issues a command to copy or cut something, the initiating
application first obtains the system Clipboard object by calling the getSys-
temClipboard() method of the java.awt.Toolkit object. Next, the applica-
tion creates a Transferable object that represents the data to be transferred.
Finally, it passes this transferable object to the clipboard by calling the set-
Contents() method of the clipboard. The initiating application must also pass
an object that implements the ClipboardOwner interface to setContents() .By
doing so, the application becomes the clipboard owner and must maintain its
Transferable object until it ceases to be the clipboard owner.
When the user issues a command to paste, the receiving application first
obtains the system Clipboard object in the same way the initiating application
did. Then it calls the getContents() method of the system clipboard to
receive the Transferable object stored there. Now it can use the methods
defined by the Transferable interface to choose a DataFlavor for the data
transfer and actually transfer the data.
When the user copies or cuts some other piece of data, a new data transfer is
initiated, and the new initiating application (it may be the same one) becomes
the new clipboard owner. The previous owner is notified it is no longer the
clipboard owner when the system invokes the lostOwnership() method of
the ClipboardOwner object specified in the initiating call to setContents() .
Note that untrusted applets are not allowed to work with the system clipboard
because there might be sensitive data on it from other applications. This means
that applets can't participate in interapplication cut-and-paste. Instead, an applet
must create a private clipboard object to use intraapplet data transfer.
Example 13-1 is a simple AWT program that demonstrates how to support String
cut-and-paste capabilities in a program. It relies on the predefined DataFla-
vor.stringFlavor and the StringSelection class, which implements the Trans-
ferable interface for string data. The program also uses the Java 1.2
DataFlavor.javaFileListFlavor to allow filenames to be pasted. Note that the
Search WWH ::




Custom Search