FileDocCategorySizeDatePackage
URLDropTargetDemo.javaAPI DocExample3252Mon Jan 09 11:02:00 GMT 2006None

URLDropTargetDemo

public class URLDropTargetDemo extends JPanel implements DropTargetListener

Fields Summary
DropTarget
dropTarget
JLabel
dropHereLabel
JTextField
statusField
static DataFlavor
urlFlavor
Constructors Summary
public URLDropTargetDemo()

        try {
            urlFlavor =
                new DataFlavor ("application/x-java-url; class=java.net.URL");
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
    
        super(new BorderLayout());
        dropHereLabel = new JLabel ("Drop here",
                                    SwingConstants.CENTER);
        dropHereLabel.setFont (getFont().deriveFont (Font.BOLD, 24.0f));
        add (dropHereLabel, BorderLayout.CENTER);
        // set up drop target stuff
        dropTarget = new DropTarget (dropHereLabel, this);
        statusField = new JTextField (30);
        statusField.setEditable(false);
        add (statusField, BorderLayout.SOUTH);
    
Methods Summary
public voiddragEnter(java.awt.dnd.DropTargetDragEvent dtde)

        System.out.println ("dragEnter");
    
public voiddragExit(java.awt.dnd.DropTargetEvent dte)

        System.out.println ("dragExit");
    
public voiddragOver(java.awt.dnd.DropTargetDragEvent dtde)

        System.out.println ("dragOver");

    
public voiddrop(java.awt.dnd.DropTargetDropEvent dtde)

        System.out.println ("drop");
        dtde.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
        Transferable trans = dtde.getTransferable();
        dumpDataFlavors (trans);
        boolean gotData = false;
        // try for application/x-java-url flavor
        try {
            if (trans.isDataFlavorSupported (urlFlavor)) {
                URL url = (URL) trans.getTransferData (urlFlavor);
                // System.out.println ("got a " + o.getClass().getName());
                statusField.setText (url.toString());
                statusField.setCaretPosition (0);
                gotData = true;
            } else if (trans.isDataFlavorSupported (DataFlavor.stringFlavor)) {
                String s = 
                    (String) trans.getTransferData (DataFlavor.stringFlavor);
                statusField.setText (s);
                gotData = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println ("gotData is " + gotData);
            dtde.dropComplete (gotData);
        }
    
public voiddropActionChanged(java.awt.dnd.DropTargetDragEvent dtde)

        System.out.println ("dropActionChanged");

    
private voiddumpDataFlavors(java.awt.datatransfer.Transferable trans)

        System.out.println ("Flavors:");
        DataFlavor[] flavors = trans.getTransferDataFlavors();
        for (int i=0; i<flavors.length; i++) {
            System.out.println ("*** " + i + ": " + flavors[i]);
        }
    
public static voidmain(java.lang.String[] args)

        JFrame frame = new JFrame ("URL DropTarget Demo");
        URLDropTargetDemo demoPanel = new URLDropTargetDemo();
        frame.getContentPane().add (demoPanel);
        frame.pack();
        frame.setVisible(true);