开发者

JList drag CustomObject but drop String

开发者 https://www.devze.com 2023-03-07 15:02 出处:网络
private JList attributesList; public AttributeGroupComponent(ArrayList<?> items) { attributesList = new JList(items.toArray());
private JList attributesList;

public AttributeGroupComponent(ArrayList<?> items) {
    attributesList = new JList(items.toArray());
    initGui();
}

private void initGui(){         
    attributesList.setDragEnabled(true);
}

then in other component I try

public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(DnDConstants.ACTION_COPY);
    Transferable tr = dtde.getTransferable();

    MyCustom开发者_开发知识库Class ac = null;

    try {
        ac = (MyCustomClass)tr.getTransferData(flavor);
        // And Here I get toString of my custom class!
        // But I expected MyCustomClass Object!
    } catch (UnsupportedFlavorException e) {
        ;// TODO
    } catch (IOException e) {
        ;// TODO
    }

    dtde.dropComplete(true);
    System.out.println("drop complete");
}


If you wanted to drag MyCustomClass from the JList to the drop component as the object itself, you would need to create a Transferable for that object.

Last year, I created something similar for all objects available in GitHub easy-dnd-swing

You would need to create your own DataFlavor that represents your object, then you setup your DragListeners and when you startDrag with the custom Transferable that you create. That transferable will contain the object you will drag.

0

精彩评论

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