|
Drag and Drop: New Data Transfer Capabilities in the JavaTM 2 Platform, Standard Edition (J2SETM), version 1.4(7) When trying out the program, be sure to try to copy and paste between native applications like your Word processor. Because you are using the system clipboard, the program will transfer whatever the contents of the clipboard are. Text ActionsWhile this article is about eXPlaining the data transfer mechanisms with the J2SE libraries, the code for copy and paste doesn't have to be that complicated when using the Swing text controls. They already know how to do cut, copy, and paste operations, among many other tasks. All you have to do is lookup the appropriate listener and add that instead. The actions associated with the text controls are retrieved through the getActions method. This returns an array of Action objects, which implement the ActionListener interface. You just need to find the specific listener and attach that instead. The following code will do just that. It replaces the creation of the two ActionListener objects and their attachment in the prior example. // get command tableAction actions[] = jt.getActions();// Find the two wantedint count = 0; for(int i =0, n = actions.length; (i<n)&& (count <2);i++) {Action a = actions[i];String name = (String)a.getValue(Action.NAME);if (name.equals(DefaultEditorKit.copyAction)) { copy.addActionListener(a); count++;} else if (name.equals(DefaultEditorKit.pasteAction)) { paste.addActionListener(a); count++;}}Transferring ImagesNow that the basic process of transferring text has been eXPlained, let's move on to something new to the 1.4 release of J2SE, images transfers. Prior versions of the J2SE didn't provide integrated support for transferring images between Java programs and native applications. While you could go through the manual process of providing the data in an understandable format, that task is no longer necessary. All you now have to do is specify that the flavor is of type DataFlavor.imageFlavor, provide the data to the clipboard as an AWT Image object, and you're all set.
|