import java.awt.* ; import javax.swing.* ; // TransferHandler import java.awt.datatransfer.* ; // DataFlavor , Transferable import java.io.* ; public class DropTest extends JFrame { JTextArea dropspot ; // view dropped as String public DropTest() { super("Drop Test") ; dropspot = new JTextArea("Drop Stuff Here") ; dropspot.setTransferHandler(new StringDropHandler()) ; this.setLayout(new BorderLayout()) ; this.getContentPane().add("Center", dropspot) ; this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) ; // exitExam() //this.setVisible(true) ; } public static void main(String[] args) { DropTest dt = new DropTest() ; dt.setSize(300,375) ; dt.setLocation(500,500) ; dt.setVisible(true) ; } //inner class /** * StringDropHandler attemps to handle all * dropped things which have a String representation. */ class StringDropHandler extends TransferHandler { /** * Can attempt drop of anything. */ public boolean canImport(JComponent comp, DataFlavor[] flavor) { return true ; } /** * Attempt import as string. */ public boolean importData(JComponent comp, Transferable t) { boolean flag = false ; // test importation try { DataFlavor[] flavors = t.getTransferDataFlavors() ; String s = "representationclass=java.lang.String" ; int p = -1 ; // which is s? for(int i = 0 ; i < flavors.length ; i++) { // UNcomment next line to see flavors favoring String //System.out.println(flavors[i].toString()) ; if (flavors[i].toString().indexOf(s) > -1) p = i ; } String content = ((String)t.getTransferData(flavors[p])).trim() ; dropspot.setText(content) ; //System.out.println(content) ; flag = true ; // got it } // otherwise tell us what is wrong catch(Exception xx) { System.out.println(xx) ; } return flag ; } } // end inner StringDropHandler } // end DropTest /* Mac drop source file: file://localhost/wwwcpp/cs245/DropTest.java Gnome drop source file: */