拖放JLabel:如何获取拖动源

时间:2011-12-19 08:46:04

标签: java swing drag-and-drop awt

我正在开发一个应用程序,可以将一个JLabel的内容放到另一个JLabel中。我使用的是NameSlot类:

public class NameSlot extends JLabel implements DropTargetListener{

NameSlot(String name){
    super(name);
    new DropTarget(this,this);

    this.setTransferHandler(new TransferHandler("foreground"));
    MouseListener listener = new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        JLabel comp = (JLabel) me.getSource();
        TransferHandler handler = comp.getTransferHandler();
        handler.exportAsDrag(comp, me, TransferHandler.COPY);
      }
    };
    this.addMouseListener(listener);
}



@Override
public void dragEnter(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void dragExit(DropTargetEvent dte) {
    // TODO Auto-generated method stub

}


@Override
public void dragOver(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void drop(DropTargetDropEvent dtde) {

//      DropTarget dt = (DropTarget) dtde.getSource();
//      NameSlot ns  = (NameSlot) dt.getComponent();

    try {
        JLabel l  =(JLabel) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
        System.out.println("drop detected from "+l.getText()+" to "+this.getText());
    } catch (UnsupportedFlavorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


@Override
public void dropActionChanged(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}

这就是我的首发方式:

    this.setLayout(null);
    NameSlot ns = new NameSlot("test");
    ns.setLocation(20, 20);
    this.add(ns);

    NameSlot ns2 = new NameSlot("test2");
    ns2.setLocation(20, 20);
    this.add(ns2);

在尝试使用dtde.getSource()并因为coolcfans注释而废弃它之后,我现在尝试使用DropTargetEvent的Transferable来获取拖动源。我得到了例外:

java.awt.datatransfer.UnsupportedFlavorException: Unicode String
    at javax.swing.TransferHandler$PropertyTransferable.getTransferData(Unknown Source)
    at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(Unknown Source)
    at sun.awt.datatransfer.TransferableProxy.getTransferData(Unknown Source)
    at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(Unknown Source)
    at an.judosoft.view.bracketSheets.factory.NameSlot.drop(NameSlot.java:71)

有人知道吗?

1 个答案:

答案 0 :(得分:2)

以下作品:

我将this.setTransferHandler(new TransferHandler("foreground"))更改为this.setTransferHandler(new TransferHandler("text"))

并使用new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String")作为DataFlavor,但它返回String而不是JLabel。如果你想要一个JLabel,你需要更改TransferHandler。

import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

public class NameSlot extends JLabel implements DropTargetListener {
    NameSlot(final String name) {
        super(name);
        new DropTarget(this, this);
        this.setTransferHandler(new TransferHandler("text"));
        final MouseListener listener = new MouseAdapter() {
            @Override
            public void mousePressed(final MouseEvent me) {
                final JLabel comp = (JLabel) me.getSource();
                System.out.println(comp);

                final TransferHandler handler = comp.getTransferHandler();
                handler.exportAsDrag(comp, me, TransferHandler.COPY);
            }
        };
        this.addMouseListener(listener);
    }

    @Override
    public void dragEnter(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    @Override
    public void dragExit(final DropTargetEvent dte) {
        // TODO Auto-generated method stub

    }

    @Override
    public void dragOver(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    @Override
    public void drop(final DropTargetDropEvent dtde) {

        // DropTarget dt = (DropTarget) dtde.getSource();
        // NameSlot ns = (NameSlot) dt.getComponent();

        try {
            final String s = (String) dtde.getTransferable().getTransferData(
                    new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String"));

            System.out.println("drop detected from " + s + " to " + this.getText());
        }
        catch (final UnsupportedFlavorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (final IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (final ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void dropActionChanged(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    public static void main(final String[] args) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        final NameSlot ns = new NameSlot("test");
        frame.add(ns);

        final NameSlot ns2 = new NameSlot("test2");
        frame.add(ns2);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}