TheDataFlavorclass allows you to specify the content type of your data. You need to specify aDataFlavorwhen fetching the data from theimportDatamethod. Several flavor types are predefined for you:
imageFlavorrepresents data in thejava.awt.Imageformat. This is used when dragging image data.stringFlavorrepresents data in the most basic form of text —java.lang.String. This is the most commonly used data flavor for most applications.javaFileListFlavorrepresentsjava.io.Fileobjects in ajava.util.Listformat. This is useful for applications that drag files, such as theTopLevelTransferHandlerexample, 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.ArrayListclass: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
Objectserialization, so the class you use to transfer the data must implement theSerializableinterface, as must anything that is serialized with it. If everything is not serializable, you will see aNotSerializableExceptionduring 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 thejavaJVMLocalObjectMimeTypeconstructor. For example, to specify a data flavor that transfers color from aDataFlavor(String)JColorChooseronly 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
ArrayListthat 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
Transferablecan 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 theArrayListand integer array flavors, together, or you can create aTransferHandlerthat accepts different types of data, such as color and text.When you create an array of
DataFlavorsto be returned from theTransferable'sgetTransferDataFlavorsmethod, 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.