TheDataFlavor
class allows you to specify the content type of your data. You need to specify aDataFlavor
when fetching the data from theimportData
method. Several flavor types are predefined for you:
imageFlavor
represents data in thejava.awt.Image
format. This is used when dragging image data.stringFlavor
represents data in the most basic form of text —java.lang.String
. This is the most commonly used data flavor for most applications.javaFileListFlavor
representsjava.io.File
objects in ajava.util.List
format. This is useful for applications that drag files, such as theTopLevelTransferHandler
example, discussed in the Top-Level Drop lesson.For most applications, this is all you need to know about data flavors. However, if you require a flavor other than these predefined types, you can create your own. If you create a custom component and want it to participate in data transfer, you will need to create a custom data flavor. The constructor for specifying a data flavor is
DataFlavor(Class, String)
. For example, to create a data flavor for thejava.util.ArrayList
class:To create a data flavor for an integer array:new DataFlavor(ArrayList.class, "ArrayList");new DataFlavor(int[].class, "Integer Array");Transferring the data using this mechanism uses
Object
serialization, so the class you use to transfer the data must implement theSerializable
interface, as must anything that is serialized with it. If everything is not serializable, you will see aNotSerializableException
during drop or copy to the clipboard.Creating a data flavor using the
DataFlavor(Class, String)
constructor allows you to transfer data between applications, including native applications. If you want to create a data flavor that transfers data only within an application, useand the
javaJVMLocalObjectMimeType
constructor. For example, to specify a data flavor that transfers color from a
DataFlavor(String)
JColorChooser
only within your application, you could use this code:String colorType = DataFlavor.javaJVMLocalObjectMimeType + ";class=java.awt.Color"; DataFlavor colorFlavor = new DataFlavor(colorType);To create a data flavor for an
ArrayList
that would work only within your application:new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=java.util.ArrayList");To create a data flavor for an integer array:
new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=\"" + int[].class.getName() + "\"");A MIME type containing special characters, such as [ or ;, must have those characters enclosed in quotes.
A
Transferable
can be implemented to support multiple flavors. For example, you can use both local and serialization flavors together, or you can use two forms of the same data, such as theArrayList
and integer array flavors, together, or you can create aTransferHandler
that accepts different types of data, such as color and text.When you create an array of
DataFlavors
to be returned from theTransferable
'sgetTransferDataFlavors
method, the flavors should be inserted in preferred order, with the most preferred appearing at element 0 of the array. Genereally the preferred order is from the richest, or most complex, form of the data down to the simpleset — the form most likely to be understood by other objects.