开发者

Custom DragAndDrop in Java

I Have a JList with custom objects. I w开发者_StackOverflow社区ould like to select the object from the list and drag and drop to a JPanel. But I'm confused about all terms in DnD tutorials. I think is there more than one way to solve it.

In the source, jlist, What are the key interfaces I have to implement and what classes do I need to call? And in the target JPanel?

Thanks


Thanks all. I solved with this approach.

Drag and Drop of complex custom objects in Java


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

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

and do create a custom 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
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜