Note: Object references don't make sense outside of the JVM. A subclass of java. For StringTransferable. Text data may have different character encodings as specified in the MIME specification. The DataFlavor should be queried for the encoding requested by the DropTarget. Here's how the Transferable can provide text data in a variety of formats and encodings:. Drag over effects provide visual feedback while the cursor is over a component, but do not permanently change the appearance of components.
Usually the DragSourceListener accomplishes drag over effects via cursor changes. There are two possible cursors:. Additionally, the definition of the dragOver and dropActionChanged methods are similar. As we shall see, these methods aren't invoked if the DropTarget rejects the operation. When the operation has ended, the DragSourceListener receives notification from a dragDropEnd message.
When so notified, the listener's responsibility is to check the success of the operation, then, if successful, perform the requested action. If the operation isn't successful there's nothing for the DragSourceListener to do.
In the case of a move action, the listener will also remove the source data. If it's a component, it will be taken out of the hierarchy; if it's the text data displayed in a text component, it will be erased.
The following is an example of dragDropEnd. If the operation isn't successful, the methods simply return. The drop action is inspected to see if it was a move operation:. Considering the complexity of the messages passed among the several objects we've discussed, it would be good to review the flow:. Here are the latest Insider stories. More Insider Sign Out. Sign In Register. Sign Out Sign In Register. Latest Insider. File objects in a java.
The format for specifying a data flavor is this: DataFlavor Class representationClass, String humanPresentableName ; For example, to create a data flavor for the java. If not everything is Serializable , you'll see a NotSerializableException during drop or copy to the clipboard. Note that creating a data flavor using the DataFlavor Class, String constructor allows you to transfer data between applications, including native applications.
Finally, 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, as you will later see. When you create the 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.
See the Components That Support DnD table in the release notes for your particular J2SE release for further details of which data types each component imports and exports. We prevously described how you can create a transfer handler that will transfer data as specified by a named property. While this is easy to do, it has limited functionality. For example, if you specify the "foreground" property, a drop would only change the color of the text.
It wouldn't change the background color. And if your component drags and drops text by default, replacing the transfer handler in this manner causes the component to lose this default ability. To solve this problem you need to write a custom TransferHandler. We have provided an example of how to create a custom transfer handler that can be installed on a component so that it can accept color on a drop. DragColorDemo specifically shows how you can drop a color onto the foreground or background of a button or label.
The example's main class can be found in DragColorDemo. The custom transfer handler is defined in ColorTransferHandler. In this example, we are only implementing import functionality and therefore only need to implement the methods canImport and importData.
A single instance of the ColorTransferHandler is created and shared by all nine buttons and the label. Color , which is the mechanism JColorChooser uses to export color. For a discussion of how data is specified to the transfer mechanism, see the previous section Specifying the Data Format.
When DragColorDemo installs the ColorTransferHandler on its components, it clobbers any pre-existing transfer handler. As discussed in Introduction to Data Transfer Support , if you install a custom transfer handler onto a component, such as JTextField , that has a Swing-provided transfer handler, you would need to re-implement the Swing support. We have provided a version of DragColorDemo , called DragColorTextFieldDemo , that creates a transfer handler that accepts color and also re-implements the clobbered support for text.
Since ColorAndTextTransferHandler must export data, it implements createTransferable , getSourceActions , and exportDone in addition to the two methods it provides for import support. The code is too long to include here, but you can find the main class's source code in DragColorTextFieldDemo.
For example, dragging a file from a drag-enabled file chooser and dropping it on a JTextArea causes the file name to be inserted into the text area, but not the contents of the file. However, a custom transfer handler that knows about javaFileListFlavor can be installed to accept the file list provided by a file chooser, open the file, read the contents, and display the contents of the file in the text area.
We have provided an example that does this. Note that because this example reads files from your local file system, launching the demo via Java Web Start will bring up a warning panel requiring permission before executing the application.
If you prefer, you can instead download the application and run it locally. The code is too long to include here, but you you can find the main class's source in DragFileDemo. DragFileDemo doesn't do anything particularly unusual with the one exception of embedding the file chooser into the main window, rather than running it from a dialog. This allows the file chooser to be interactive without blocking the rest of the application.
As you have seen before, stringFlavor is necessary because the new transfer handler clobbers the default behavior for the text area and this re-implements its basic behavior.
In the importData method for the transfer handler, the code first checks to see if files are being imported. If so, the files are opened into a BufferedReader and the contents are appended. If the imported data is not files, it then checks for strings. Data Transfer with a Custom Component We have seen how to customize data transfer for standard Swing components, but how do you add data transfer to a custom component?
The simplest data transfer to implement is drag and drop: You first determine which gesture initiates the drag. A mouse press? A press and drag? How many pixels must be traversed to define a valid drag? When drag conditions are met, you must invoke exportAsDrag on the component's TransferHandler. The DragPictureDemo example shows how to implement full data transfer with a custom component. You can find the main class's source code in DragPictureDemo.
The custom component DTPicture is a subclass of the Picture component, modified to support data transfer. The MouseMotionListener interface allows you to detect mouse motion by implementing the mouseDragged method. We arbitrarily chose a displacement of 5 pixels to determine whether the user is actually attempting to drag, as opposed to clicking on a picture.
Once the cursor has moved a distance of 5 pixels in either direction while the mouse button is down, the transfer handler is called to initiate the drag. The PictureTransferHandler class looks very much like other custom transfer handlers you have seen except this one transfers data using the built-in support for java.
For more information, see Specifying the Data Format. Data Transfer with a Custom DataFlavor By now you've seen several examples of transfer handlers that transfer data using conventional formats. ArrayList class. To achieve this, a custom Transferable is created. To implement a Transferable you must conform to the Transferable interface and provide implementations for the methods getTransferData , getTransferDataFlavors and isDataFlavorSupported.
The DragListDemo. On each list is installed a shared instance of ArrayListTransferHandler. The basic steps are: Ensure a transfer handler is installed on the component.
The latter approach proves to be easy to implement with text components more later but requires somewhat more work with other components, since you need logic to determine which component to fire the action on. A nice feature of the DefaultEditorKit methods is that they remember which component last had the focus.
CutAction ; menuItem. CopyAction ; menuItem. PasteAction ; menuItem. For any non-text component, you must manually set up the bindings in the input and action maps. NAME ; imap. NAME ; menuItem. NAME ; ties the copy action to the menu item. Why does this D'n'D code import nothing from the java. Take a look at [this other stack overflow question][1].
I think it has some of what you're looking for. Thorn is there something in java 1. Add a comment. Active Oldest Votes. MadProgrammer MadProgrammer k 22 22 gold badges silver badges bronze badges. Bookmarking this page just for this answer. Very nice, and thanks! MadProgrammer Thanks. Real nice. What editor are you using? Show 14 more comments. Edit 2 import java. DataFlavor; import java. UnsupportedFlavorException; import java. File; import java.
IOException; import java. List; import javax. Instead edit your original question with an addendum, and then comment to let me know that you've done this. Actually, unless it's change in Java 7, I always get java.
List which contains File objects - I've not begin able to get generics to work for this ; — MadProgrammer. MadProgrammer: you're probably correct. Let me edit my answer. I edited the main code and put back in my transferHandler.
Its right under the JList implementation — rogerthat. You don't need or use a MouseListener for this type of behavior, and you certainly can't guess at how to do this.
Please see my Edit 2 for some sample code.
0コメント