gpt4 book ai didi

java - Transferhandler 中的 getTransferData() 无法按预期多次删除 jtree 中的多个对象

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:06 28 4
gpt4 key购买 nike

我正在尝试将多行(不连续)从 jtable 拖动到 jtree。我正在使用自定义的可传输对象和自定义的传输处理程序。

但是,我在导入数据方法中丢失了对象,因为它在类 DropTargetContext.java 内的 java 方法 getTransferData(DataFlavor df) 中丢失。

代码如下:

public class cObjectList extends ArrayList<cObject> implements Transferable, Serializable {    

public static DataFlavor OBJECT_LIST_FLAVOR = new DataFlavor(cObjectList.class, "Object List");
private DataFlavor flavors[] = { OBJECT_LIST_FLAVOR };

@Override
public DataFlavor[] getTransferDataFlavors() { return flavors; }//end method getTransferDataFlavors

@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.getRepresentationClass() == cObjectList .class; }//end method isDataFlavorSupported

@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) { return this; } else {
throw new UnsupportedFlavorException(flavor); }//end else
}//end method getTransferData
}//end class cObjectList

cObject 类是可序列化的。定制的传输处理程序被声明为包含树和表的面板中的内部类。 */

class ObjectTreeTransferHandler extends TransferHandler{
/*the createTransferable method successfully creates the transfer object */
@Override
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
if (support.isDataFlavorSupported(cObjectList.OBJECT_LIST_FLAVOR)) {
cObjectList obList = null;
try {
Transferable t = support.getTransferable();
obList = (cObjectList) t.getTransferData(cObjectList.OBJECT_LIST_FLAVOR);
} catch (UnsupportedFlavorException ufe) {
System.out.println("UnsupportedFlavor: " + ufe.getMessage());
return false;
} catch (java.io.IOException ioe) {
new myException(ioe);
return false;
}

if (obList != null && support.getComponent() instanceof myJTree) {
//do work
}//end if
}//end inner class ObjectTreeTransferHandler

可传输对象t正确地具有数组列表中的对象,但是当我调用t.getTransferData时,数组列表返回null对象。例如。如果从表中选择了三行,则会调用 cObjectList 中的 getTransferData 方法,该方法会正确返回数组列表中的这些对象,但当它到达 DropTargetContext.java 中的 getTransferData 时,数组列表中的值将返回为 null,即使列表/对象本身仍然显示 size =3

为树选择的放置模式是:

treeObjectStructure.setTransferHandler(new ObjectTreeTransferHandler()); treeObjectStructure.setDropMode(DropMode.ON_OR_INSERT);

谁能帮我弄清楚我做错了什么?

最佳答案

我赞成上面的答案。我遇到了同样的问题,我只是用莱尔所说的解决了它:

class TransferablePanel implements Transferable {
protected static DataFlavor PANELFLAVOR = new DataFlavor(Object.class, "A
JPanel Object");//instead JPanel.class I used Object.class ****
protected static DataFlavor[] supportedFlavors = { PANELFLAVOR };
JPanel panel;
public TransferablePanel(JPanel panel) {
this.panel = panel;
}

@Override
public DataFlavor[] getTransferDataFlavors() {
return supportedFlavors;
}

@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (flavor.equals(PANELFLAVOR))
return true;
return false;
}

@Override
public Object getTransferData(DataFlavor flavor) throws
UnsupportedFlavorException {

if (flavor.equals(PANELFLAVOR)){
return panel;
}
else
throw new UnsupportedFlavorException(flavor);
}
}

关于java - Transferhandler 中的 getTransferData() 无法按预期多次删除 jtree 中的多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13730660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com