import java.awt.BorderLayout; 
import java.awt.datatransfer.DataFlavor; 
import java.awt.datatransfer.Transferable; 
import java.awt.datatransfer.UnsupportedFlavorException; 
import java.awt.dnd.DnDConstants; 
import java.awt.dnd.DragGestureEvent; 
import java.awt.dnd.DragGestureListener; 
import java.awt.dnd.DragSource; 
import java.awt.dnd.DragSourceAdapter; 
import java.awt.dnd.DragSourceDragEvent; 
import java.awt.dnd.DragSourceEvent; 
import java.awt.dnd.DragSourceListener; 
import java.awt.dnd.DropTargetAdapter; 
import java.awt.dnd.DropTargetDropEvent; 
import java.awt.event.ActionEvent; 
import java.io.IOException; 

import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JTextArea; 

public class Drag { 

    private static JTextArea console = new JTextArea(); 

    static public void main(String[] s) { 
        JFrame f = new JFrame(); 
        f.setSize(800, 600); 
        f.getContentPane().setLayout(new BorderLayout()); 
        f.getContentPane().add(new JButton(openDialog(f)), BorderLayout.NORTH); 
        f.getContentPane().add(console, BorderLayout.CENTER); 
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        f.setLocationRelativeTo(null); 
        f.setVisible(true); 
    } 

    private static Action openDialog(JFrame f) { 
        return new AbstractAction("open Drag'n'Drop Dialog"){ 
            public void actionPerformed(ActionEvent e) { 
                JDialog dialog = new JDialog(f); 
                dialog.getContentPane().setLayout(new BorderLayout()); 
                JList<String> list = new JList<String>(new String[]{"drag me ->"}); 
DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(list, DnDConstants.ACTION_COPY_OR_MOVE, new DragGesture()); 

                JLabel lbl = new JLabel("drop here"); 
                lbl.setDropTarget(new DropTarget(lbl)); 
                dialog.getContentPane().add(list, BorderLayout.WEST); 
                dialog.getContentPane().add(lbl, BorderLayout.CENTER); 
                dialog.setSize(300, 180); 
                dialog.setLocationRelativeTo(f); 
                dialog.setVisible(true); 
            } 

        }; 
    } 

    static class DragGesture implements DragGestureListener { 
        public void dragGestureRecognized(DragGestureEvent dge) { 
            console.setText(console.getText() + "\ndragGesture: \t" + dge.toString()); 
            Transferable transferable =new Transferable() { 
                public boolean isDataFlavorSupported(DataFlavor flavor) { 
                    return true; 
                } 
                public DataFlavor[] getTransferDataFlavors() { 
                    return new DataFlavor[]{DataFlavor.stringFlavor}; 
                } 
                public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 
                    return "transferData"; 
                } 
            }; 
            DragSourceListener sourceListener = new DragSourceAdapter() { 
                public void dragExit(DragSourceEvent dse) { 
                    console.setText(console.getText() + "\ndragExit: \t" + dse.toString()); 
                } 
                public void dragEnter(DragSourceDragEvent dsde) { 
                    console.setText(console.getText() + "\ndragEnter: \t" + dsde.toString()); 
                } 
            }; 
            dge.startDrag(DragSource.DefaultCopyDrop, transferable, sourceListener); 
        } 
    } 

    static class DropTarget extends java.awt.dnd.DropTarget { 
        public DropTarget(JLabel lbl) { 
            super(lbl, new DropTargetAdapter() { 
                public void drop(DropTargetDropEvent dtde) { 
                    try { 
lbl.setText(dtde.getTransferable().getTransferData(DataFlavor.stringFlavor).toString()); 
                    } catch (Exception e) { 
                        console.setText(e.getMessage()); 
                    } 
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 
                } 
            }); 
        } 
    } 

} 