import java.applet.Applet; import java.awt.*; import java.awt.dnd.*; import java.awt.datatransfer.*; import java.awt.event.*; import java.io.*; public class DnDTest extends Frame implements DragGestureListener, DragSourceListener, DropTargetListener { private List dragList = new List(); private List dropList = new List(); private DragSource dragSource = new DragSource(); //An object representing the component were the drop will take place. private DropTarget dropTarget = new DropTarget( dropList, // The drop Component DnDConstants.ACTION_COPY, // actions on drop this); // DropTargetListener //An object representing a drag gesture. In Windows that is //pressing the mouse and moving it. The given DragGestureListener //will be called when the user starts dragging. DragGestureRecognizer recognizer = dragSource.createDefaultDragGestureRecognizer(dragList, // The Component where the drag starts. DnDConstants.ACTION_COPY, // actions on drop this); // DragGestureListener public DnDTest() { super("Drag and Drop Test"); dragList.add("one"); dragList.add("two"); dragList.add("three"); dragList.add("four"); dragList.add("five"); dragList.add("six"); setLayout(new GridLayout(2,0)); add(new Label("Drag Source")); add(dragList); add(new Label("Drop Target")); add(dropList); } public static void main(String[] args) { Frame f = new DnDTest(); f.setBounds(300,300,200,175); f.setVisible(true); f.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void dragGestureRecognized(DragGestureEvent e) { System.out.println("drag gesture recognized"); //This method is called when the user starts dragging. String selectedItem = dragList.getSelectedItem(); //Start the drag by calling the startDrag() method. e.startDrag( DragSource.DefaultCopyDrop, // cursor new StringSelection(selectedItem), // transferable this); // DragSourceListener } public void dragDropEnd(DragSourceDropEvent e) { System.out.println("source drag drop end"); } public void dragEnter(DragSourceDragEvent e) { System.out.println("source drag enter"); } public void dragExit(DragSourceEvent e) { System.out.println("source drag exit"); } public void dragOver(DragSourceDragEvent e) { System.out.println("source drag over"); } public void dropActionChanged(DragSourceDragEvent e) { System.out.println("source drop action changed"); } public void dragEnter(DropTargetDragEvent e) { System.out.println("target drag enter"); //The user has entered this component. } public void drop(DropTargetDropEvent e) { System.out.println("target drop"); //The user has dropped in this component. try { //If the dropped object is of the right flavor, we //get it and place it in the list. if(e.isDataFlavorSupported(DataFlavor.stringFlavor)){ Transferable tr = e.getTransferable(); e.acceptDrop (DnDConstants.ACTION_COPY); String s = (String)tr.getTransferData (DataFlavor.stringFlavor); dropList.add(s); e.dropComplete(true); } else { e.rejectDrop(); } } catch (IOException io) { io.printStackTrace(); e.rejectDrop(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); e.rejectDrop(); } } public void dragExit(DropTargetEvent e) { System.out.println("target drag exit"); } public void dragOver(DropTargetDragEvent e) { System.out.println("target drag over"); } public void dropActionChanged(DropTargetDragEvent e) { System.out.println("target drop action changed"); } }