Using and Creating a DataFlavor
The DataFlavor class allows you to specify the content type of your data. You need to specify a DataFlavor when fetching the data from the importData method. Several flavor types are predefined for you:
- imageFlavor represents data in the java.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 represents java.io.File objects in a java.util.List format. This is useful for applications that drag files, such as the TopLevelTransferHandler 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 the java.util.ArrayList class:
new DataFlavor(ArrayList.class, "ArrayList");
To create a data flavor for an integer array:
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 the Serializable interface, as must anything that is serialized with it. If everything is not serializable, you will see a NotSerializableException 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, use javaJVMLocalObjectMimeType and the DataFlavor(String) constructor. For example, to specify a data flavor that transfers color from a 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 the ArrayList and integer array flavors, together, or you can create a TransferHandler that accepts different types of data, such as color and text.
When you create an array of DataFlavors to be returned from the Transferable ‘s getTransferDataFlavors method, the flavors should be inserted in preferred order, with the most preferred appearing at element 0 of the array. Generally the preferred order is from the richest, or most complex, form of the data down to the simple set — the form most likely to be understood by other objects.
Dataflavor class in java
A DataFlavor provides meta information about data. DataFlavor is typically used to access data on the clipboard, or during a drag and drop operation. An instance of DataFlavor encapsulates a content type as defined in RFC 2045 and RFC 2046. A content type is typically referred to as a MIME type. A content type consists of a media type (referred to as the primary type), a subtype, and optional parameters. See RFC 2045 for details on the syntax of a MIME type. The JRE data transfer implementation interprets the parameter «class» of a MIME type as a representation class. The representation class reflects the class of the object being transferred. In other words, the representation class is the type of object returned by Transferable.getTransferData(java.awt.datatransfer.DataFlavor) . For example, the MIME type of imageFlavor is «image/x-java-image;class=java.awt.Image» , the primary type is image , the subtype is x-java-image , and the representation class is java.awt.Image . When getTransferData is invoked with a DataFlavor of imageFlavor , an instance of java.awt.Image is returned. It’s important to note that DataFlavor does no error checking against the representation class. It is up to consumers of DataFlavor , such as Transferable , to honor the representation class.
Note, if you do not specify a representation class when creating a DataFlavor , the default representation class is used. See appropriate documentation for DataFlavor ‘s constructors. Also, DataFlavor instances with the «text» primary MIME type may have a «charset» parameter. Refer to RFC 2046 and selectBestTextFlavor(java.awt.datatransfer.DataFlavor[]) for details on «text» MIME types and the «charset» parameter. Equality of DataFlavors is determined by the primary type, subtype, and representation class. Refer to equals(DataFlavor) for details. When determining equality, any optional parameters are ignored. For example, the following produces two DataFlavors that are considered identical:
DataFlavor flavor1 = new DataFlavor(Object.class, "X-test/test; foo=bar"); DataFlavor flavor2 = new DataFlavor(Object.class, "X-test/test; x=y"); // The following returns true. flavor1.equals(flavor2);
As mentioned, flavor1 and flavor2 are considered identical. As such, asking a Transferable for either DataFlavor returns the same results. For more information on the using data transfer with Swing see the How to Use Drag and Drop and Data Transfer, section in Java Tutorial.
Field Summary
To transfer a list of files to/from Java (and the underlying platform) a DataFlavor of this type/subtype and representation class of java.util.List is used.
To transfer a reference to an arbitrary Java object reference that has no associated MIME Content-type, across a Transferable interface WITHIN THE SAME JVM, a DataFlavor with this type/subtype is used, with a representationClass equal to the type of the class/interface being passed across the Transferable .
In order to pass a live link to a Remote object via a Drag and Drop ACTION_LINK operation a Mime Content Type of application/x-java-remote-object should be used, where the representation class of the DataFlavor represents the type of the Remote interface to be transferred.
A MIME Content-Type of application/x-java-serialized-object represents a graph of Java object(s) that have been made persistent.
as of 1.3. Use DataFlavor.getReaderForText(Transferable) instead of Transferable.getTransferData(DataFlavor.plainTextFlavor) .
Class DataFlavor
A DataFlavor provides meta information about data. DataFlavor is typically used to access data on the clipboard, or during a drag and drop operation.
An instance of DataFlavor encapsulates a content type as defined in RFC 2045 and RFC 2046. A content type is typically referred to as a MIME type.
A content type consists of a media type (referred to as the primary type), a subtype, and optional parameters. See RFC 2045 for details on the syntax of a MIME type.
The JRE data transfer implementation interprets the parameter «class» of a MIME type as a representation class. The representation class reflects the class of the object being transferred. In other words, the representation class is the type of object returned by Transferable.getTransferData(java.awt.datatransfer.DataFlavor) . For example, the MIME type of imageFlavor is «image/x-java-image;class=java.awt.Image» , the primary type is image , the subtype is x-java-image , and the representation class is java.awt.Image . When getTransferData is invoked with a DataFlavor of imageFlavor , an instance of java.awt.Image is returned. It’s important to note that DataFlavor does no error checking against the representation class. It is up to consumers of DataFlavor , such as Transferable , to honor the representation class.
Note, if you do not specify a representation class when creating a DataFlavor , the default representation class is used. See appropriate documentation for DataFlavor ‘s constructors.
Also, DataFlavor instances with the «text» primary MIME type may have a «charset» parameter. Refer to RFC 2046 and selectBestTextFlavor(java.awt.datatransfer.DataFlavor[]) for details on «text» MIME types and the «charset» parameter.
Equality of DataFlavors is determined by the primary type, subtype, and representation class. Refer to equals(DataFlavor) for details. When determining equality, any optional parameters are ignored. For example, the following produces two DataFlavors that are considered identical:
DataFlavor flavor1 = new DataFlavor(Object.class, "X-test/test; foo=bar"); DataFlavor flavor2 = new DataFlavor(Object.class, "X-test/test; x=y"); // The following returns true. flavor1.equals(flavor2);
As mentioned, flavor1 and flavor2 are considered identical. As such, asking a Transferable for either DataFlavor returns the same results.
For more information on using data transfer with Swing see the How to Use Drag and Drop and Data Transfer, section in The Java Tutorial.
Dataflavor class in java
A DataFlavor provides meta information about data. DataFlavor is typically used to access data on the clipboard, or during a drag and drop operation. An instance of DataFlavor encapsulates a content type as defined in RFC 2045 and RFC 2046. A content type is typically referred to as a MIME type. A content type consists of a media type (referred to as the primary type), a subtype, and optional parameters. See RFC 2045 for details on the syntax of a MIME type. The JRE data transfer implementation interprets the parameter «class» of a MIME type as a representation class. The representation class reflects the class of the object being transferred. In other words, the representation class is the type of object returned by Transferable.getTransferData(java.awt.datatransfer.DataFlavor) . For example, the MIME type of imageFlavor is «image/x-java-image;class=java.awt.Image» , the primary type is image , the subtype is x-java-image , and the representation class is java.awt.Image . When getTransferData is invoked with a DataFlavor of imageFlavor , an instance of java.awt.Image is returned. It’s important to note that DataFlavor does no error checking against the representation class. It is up to consumers of DataFlavor , such as Transferable , to honor the representation class.
Note, if you do not specify a representation class when creating a DataFlavor , the default representation class is used. See appropriate documentation for DataFlavor ‘s constructors. Also, DataFlavor instances with the «text» primary MIME type may have a «charset» parameter. Refer to RFC 2046 and selectBestTextFlavor(java.awt.datatransfer.DataFlavor[]) for details on «text» MIME types and the «charset» parameter. Equality of DataFlavors is determined by the primary type, subtype, and representation class. Refer to equals(DataFlavor) for details. When determining equality, any optional parameters are ignored. For example, the following produces two DataFlavors that are considered identical:
DataFlavor flavor1 = new DataFlavor(Object.class, "X-test/test; foo=bar"); DataFlavor flavor2 = new DataFlavor(Object.class, "X-test/test; x=y"); // The following returns true. flavor1.equals(flavor2);
As mentioned, flavor1 and flavor2 are considered identical. As such, asking a Transferable for either DataFlavor returns the same results. For more information on the using data transfer with Swing see the How to Use Drag and Drop and Data Transfer, section in Java Tutorial.
Field Summary
To transfer a list of files to/from Java (and the underlying platform) a DataFlavor of this type/subtype and representation class of java.util.List is used.
To transfer a reference to an arbitrary Java object reference that has no associated MIME Content-type, across a Transferable interface WITHIN THE SAME JVM, a DataFlavor with this type/subtype is used, with a representationClass equal to the type of the class/interface being passed across the Transferable .
In order to pass a live link to a Remote object via a Drag and Drop ACTION_LINK operation a Mime Content Type of application/x-java-remote-object should be used, where the representation class of the DataFlavor represents the type of the Remote interface to be transferred.
A MIME Content-Type of application/x-java-serialized-object represents a graph of Java object(s) that have been made persistent.
as of 1.3. Use DataFlavor.getReaderForText(Transferable) instead of Transferable.getTransferData(DataFlavor.plainTextFlavor) .