Java中的自定义DragAndDrop

时间:2011-02-21 17:54:22

标签: java swing drag-and-drop

我有一个带有自定义对象的JList。我想从列表中选择对象并拖放到JPanel。但我对DnD教程中的所有术语感到困惑。我认为解决它的方法不止一种。

在源代码中,jlist,我必须实现哪些关键接口以及需要调用哪些类? 而在目标JPanel?

由于

2 个答案:

答案 0 :(得分:1)

谢谢大家。我用这种方法解决了。

Drag and Drop of complex custom objects in Java

答案 1 :(得分:0)

测试http://www.javaworld.com/javatips/jw-javatip97.html?page=1

  public void dragGestureRecognized(DragGestureEvent e) {
  .....e.startDrag(cursor, new TransferableElement2D(myTransfer), this);

并创建自定义Transferable

 public class TransferableElement2D implements Transferable {
    /*objecto to transfer with dnd*/
         Element2D element2d;
     public TransferableElement2D(Element2D element2d) {
    this.element2d = element2d;
     }
        public synchronized DataFlavor[] getTransferDataFlavors() {
    return new DataFlavor[] { DataFlavor.stringFlavor };
   }
public boolean isDataFlavorSupported(DataFlavor flavor) {
    return true;
}
public synchronized Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException, IOException {
    if (isDataFlavorSupported(flavor)) {
        return element2d;
    } else {
        throw new UnsupportedFlavorException(flavor);//19209ea19209ea
    }
}

}