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");
}
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();
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想将
MyCustomClass
作为对象本身从 JList 拖动到放置组件,则需要为该对象创建一个Transferable
。去年,我为 GitHub 中可用的所有对象创建了类似的东西 easy-dnd-swing
需要创建代表您的对象的自己的 DataFlavor,然后设置 DragListeners,当您 startDrag 使用您创建的自定义 Transferable。该可传输文件将包含您将拖动的对象。
If you wanted to drag
MyCustomClass
from the JList to the drop component as the object itself, you would need to create aTransferable
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.