使用对象从 JList / JTable 中拖放
我在使用 Java 进行拖放时遇到一些问题。
我认为这非常简单,我只想允许将自定义对象从 JList 拖放到另一个 JList 中。
我已经阅读了很多相关资源,现在我有了这段代码:
listPlantation = new JList();
plantationList = Plantation.plantations;
DefaultListModel dlm = new DefaultListModel();
Iterator<Plantation> it = plantationList.iterator();
// Here I instance my ListModel with my custom Element
while(it.hasNext())
{
Plantation obj = it.next();
if(obj !=null)
{
dlm.addElement(obj);
}
}
// Yeah of course I want Drag Enable :)
listPlantation.setDragEnabled(true);
// Ok let's create my Transferhandler
listPlantation.setTransferHandler(new TransferHandler() {
@Override
protected Transferable createTransferable(JComponent c) {
JList list = (JList) c;
Object vin = list.getSelectedValue();
if(vin instanceof VinAbstract)
{
System.out.println(vin);
// I created my Own transferable object for my custom object
return new TransferableVinAbstract((VinAbstract) vin);
}
return null;
}
@Override
public int getSourceActions(JComponent c) {
return COPY; // COPY MOVE COPY_OR_MOVE same problem :)
}
});
listPlantation.setModel(dlm);
如果我注释 SetTransferHandler 部分,它就可以工作,但我只有对象的字符串表示形式。
当 SetTransferHandler 部分为“On”时,当我拖动对象时,在 createTransferable >> System.out.println(vin) 打印一些不错的东西给我。
最奇怪的是,我对 JTree 使用了或多或少相同的代码,并且它可以工作,但它也不适用于 JTable...
无论如何,感谢您的反映!
I have some problems with Drag and Drop with Java.
It's pretty simple I think, I just want to allow a Drag drag and drop custom object from a JList to another.
I've read lot of resources about that and now I have this code :
listPlantation = new JList();
plantationList = Plantation.plantations;
DefaultListModel dlm = new DefaultListModel();
Iterator<Plantation> it = plantationList.iterator();
// Here I instance my ListModel with my custom Element
while(it.hasNext())
{
Plantation obj = it.next();
if(obj !=null)
{
dlm.addElement(obj);
}
}
// Yeah of course I want Drag Enable :)
listPlantation.setDragEnabled(true);
// Ok let's create my Transferhandler
listPlantation.setTransferHandler(new TransferHandler() {
@Override
protected Transferable createTransferable(JComponent c) {
JList list = (JList) c;
Object vin = list.getSelectedValue();
if(vin instanceof VinAbstract)
{
System.out.println(vin);
// I created my Own transferable object for my custom object
return new TransferableVinAbstract((VinAbstract) vin);
}
return null;
}
@Override
public int getSourceActions(JComponent c) {
return COPY; // COPY MOVE COPY_OR_MOVE same problem :)
}
});
listPlantation.setModel(dlm);
If I comment the SetTransferHandler part, it's working , but I only have the String representation of my object.
And when SetTransferHandler part is "On", when I drag my object, in createTransferable >> System.out.println(vin) print me somethings good.
The weirdest things is that I use more or less the same code for a JTree and it's working, but it's also doesn't work with JTable....
Thanks for your reflexion anyway !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要编写一个自定义渲染器来处理您的对象。
I think you need to code a custom renderer to handle your object.