2011-05-23 16 views
-1
private JList attributesList; 

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

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

然後在其他組件我嘗試JList的阻力CustomObject但下降字符串

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"); 
} 
+3

你的具體問題是什麼? – jzd 2011-05-23 12:13:21

+2

另外,你使用什麼味道? SSCCE會很好。 – jzd 2011-05-23 12:14:22

回答

1

如果你想從JList中拖動MyCustomClass的下降成分爲對象本身,你需要創建一個Transferable爲那個對象。

去年,我創建了所有對象是否有類似的東西在GitHub上easy-dnd-swing

您將需要創建自己的DataFlavor表示你的對象,那麼你設置你的DragListeners,當你startDrag與自定義轉換,你創建。那個可轉移的將包含你將拖動的對象。