import java.awt.*; import java.awt.event.*; import java.awt.dnd.*; import java.awt.datatransfer.*; import javax.swing.*; import java.io.*; public class Drag1 extends JFrame implements DragGestureListener, DragSourceListener, DropTargetListener { static String items[] = { "Item 1", "Item No. 2", "The Third Item!" }; Container cpane; DropTarget mytarget; DragSource mysource; DragGestureRecognizer dgr; JLabel source1; JList list1; JTextArea text1; DefaultListModel lmod; public Drag1() { super("Drag1 Test"); cpane = getContentPane(); cpane.setLayout(new BorderLayout()); source1 = new JLabel("Drag Source Label - drag me!"); lmod = new DefaultListModel(); list1 = new JList(lmod); for(int i = 0; i < items.length; i++) lmod.addElement(items[i]); text1 = new JTextArea("Drag from the label to\nthe list, then drop!", 5, 24); mysource = new DragSource(); mytarget = new DropTarget(list1, DnDConstants.ACTION_COPY_OR_MOVE, this); dgr = mysource.createDefaultDragGestureRecognizer(source1, DnDConstants.ACTION_COPY_OR_MOVE, this); cpane.add("North", source1); cpane.add("Center", list1); cpane.add("South", text1); text1.setBackground(Color.pink); setBounds(100, 100, 320, 460); setVisible(true); } public void dragGestureRecognized(DragGestureEvent e) { String sel = source1.getText(); e.startDrag(DragSource.DefaultCopyDrop, new StringSelection(sel), this); } public void dragEnter(DropTargetDragEvent e) { System.out.println("dragEnter"); e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); } public void drop(DropTargetDropEvent e) { System.out.println("drop"); try { if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) { Transferable tr = e.getTransferable(); e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); String s = (String)tr.getTransferData(DataFlavor.stringFlavor); lmod.addElement(s); e.dropComplete(true); } else { e.rejectDrop(); } } catch (Exception ex) { System.out.println("Data transfer exception: " + ex); } } public void dragExit(DropTargetEvent e) { System.out.println("dragExit"); } public void dragOver(DropTargetDragEvent e) { System.out.println("dragOver (drop)"); } public void dropActionChanged(DropTargetDragEvent e) { System.out.println("dropActionChanged"); } public void dragDropEnd(DragSourceDropEvent e) { System.out.println("dragDropEnd"); } public void dragEnter(DragSourceDragEvent e) { System.out.println("dragEnter"); } public void dragExit(DragSourceEvent e) { System.out.println("dragExit"); } public void dragOver(DragSourceDragEvent e) { System.out.println("dragOver (drag)"); } public void dropActionChanged(DragSourceDragEvent e) { System.out.println("dragActionChanged (drag)"); } public static void main(String [] args) { Drag1 d1; d1 = new Drag1(); d1.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } } ); } }