Data Transfer

Contents:
DataFlavor
Transferable Interface
ClipboardOwner Interface
Clipboard
StringSelection
UnsupportedFlavorException
Reading and Writing the Clipboard

One feature that was missing from Java 1.0 was the ability to access the system clipboard. It was impossible to cut and paste data from one program into another. Java 1.1 includes a package called java.awt.datatransfer that supports clipboard operations. Using this package, you can cut an arbitrary object from one program and paste it into another. In theory, you can cut and paste almost anything; in practice, you usually want to cut and paste text strings, so the package provides special support for string operations. The current version allows only one object to be on the clipboard at a time.

java.awt.datatransfer consists of three classes, two interfaces, and one exception. Objects that can be transferred implement the Transferable interface. The Transferable interface defines methods for working with different flavors of an object. The concept of flavors is basic to Java's clipboard model. Essentially, a flavor is a MIME content type. Any object can be represented in several different ways, each corresponding to a different MIME type. For example, a text string could be represented by a Java String object, an array of Unicode character data, or some kind of rich text that contains font information. The object putting the string on the clipboard provides whatever flavors it is capable of; an object pasting the string from the clipboard takes whatever flavor it can handle. Flavors are represented by the DataFlavor class, and the UnsupportedFlavorException is used when an object asks for a DataFlavor that is not available.

The Clipboard class represents the clipboard itself. There is a single system clipboard, but you can create as many private clipboards as you want. The system clipboard lets you cut and paste between arbitrary applications (for example, Microsoft Word and some Java programs). Private clipboards are useful within a single application, though you could probably figure out some way to export a clipboard to another application using RMI.

To put data on the clipboard, you must implement the ClipboardOwner interface, which provides a means for you to be notified when the data you write is removed from the clipboard. (There isn't any ClipboardReader interface; any object can read from the clipboard.) The final component of the datatransfer package is a special class called StringSelection that facilitates cutting and pasting text strings.

Cutting and pasting isn't the whole story; JavaSoft has also promised drag-and-drop capabilities, but this won't be in the initial release of Java 1.1.

DataFlavor

A DataFlavor represents a format in which data can be transferred. The DataFlavor class includes two common data flavors; you can create other flavors by extending this class. Flavors are essentially MIME content types and are represented by the standard MIME type strings. An additional content subtype has been added to represent Java classes; the content type of a Java object is:[1]

[1] The type name changed to x-java-serialized-object in the 1.1.1 release.

application/x-java-serialized-object <classname> 

For example, the content type of a Vector object would be:

application/x-java-serialized-object java.util.Vector 

In addition to the content type, a DataFlavor also contains a presentable name. The presentable name is intended to be more comprehensible to humans than the MIME type. For example, the presentable name of a VectorFlavor object might just be "Vector", rather than the complex and lengthy MIME type given previously. Presentable names are useful when a program needs to ask the user which data flavor to use.

DataFlavor Methods

Variables

The DataFlavor class includes two public variables that hold "prebuilt" flavors representing different kinds of text objects. These flavors are used in conjunction with the StringSelection class. Although these flavors are variables for all practical purposes, they are used as constants.

Constructors

The DataFlavor class has two constructors. One creates a DataFlavor given a MIME content type; the other creates a DataFlavor given a Java class and builds the MIME type from the class name.

Presentations Protected methods Miscellaneous methods