JList拖动CustomObject但拖放String

|
private JList attributesList;

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

private void initGui(){         
    attributesList.setDragEnabled(true);
}
然后在其他组件中尝试
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(DnDConstants.ACTION_COPY);
    Transferable tr = dtde.getTransferable();

    MyCustomClass 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\");
}
    
已邀请:
如果要将
MyCustomClass
从JList拖动到放置组件作为对象本身,则需要为该对象创建
Transferable
。 去年,我为GitHub easy-dnd-swing中的所有可用对象创建了类似内容 您将需要创建自己的代表对象的DataFlavor,然后设置DragListeners,并在使用创建的自定义Transferable进行startDrag时设置。该可转移对象将包含您将拖动的对象。     

要回复问题请先登录注册