简体   繁体   English

Transferhandler中的getTransferData()无法按预期方式在jtree中多次删除多个对象

[英]getTransferData() in Transferhandler not working as expected for multiple dropping multiple objects in jtree

I am trying to drag multiple rows (discontinuous ) from a jtable to a jtree. 我试图将多行(不连续的)从jtable拖到jtree。 I am using a customized transferable object and a customized transferhandler. 我正在使用自定义的可转移对象和自定义的转移处理程序。

However, I lose my objects within the import data method, as it gets lost inside the java method getTransferData(DataFlavor df) within class DropTargetContext.java . 但是,我在导入数据方法中丢失了对象,因为它在DropTargetContext.java类的java方法getTransferData(DataFlavor df)丢失了。

The code is as follows: 代码如下:

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

The class cObject is serializable. 类cObject是可序列化的。 The customized transfer handler is declared as an inner class within the panel that contains both tree and table. 自定义的传输处理程序在包含树和表的面板中被声明为内部类。 */ * /

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

The transferable object t has the objects in array list correctly, but when I call t.getTransferData , the array list comes back the null objects. 可传输对象t在数组列表中正确包含了对象,但是当我调用t.getTransferData ,数组列表又返回了null对象。 Eg if three rows were chosen from the table, This calls getTransferData method in cObjectList which returns these objects in the arraylist correctly but when it reaches getTransferDat a in DropTargetContext.java the values inside the array list return as null even thought the list/ object itself still shows size =3 . 例如,如果从表中选择了三行,这将调用cObjectList中的getTransferData方法,该方法正确地返回了arraylist中的这些对象,但是当到达DropTargetContext.java中的getTransferDat a时,即使列表/对象本身也返回了null仍显示size =3

The drop mode selected for the tree is: 为树选择的放置模式是:

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

Can anyone help me figure out what I have done wrong? 谁能帮我弄清楚我做错了什么?

I upvote the answer above. 我支持上面的答案。 I had the same problem and I just fixed it using what Lyle said: 我有同样的问题,我只是用莱尔说的来解决:

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);
  }
 }

I had a similar problem, and it turned out that the first argument to the DataFlavor constructor has to be Object.class. 我有一个类似的问题,结果证明DataFlavor构造函数的第一个参数必须是Object.class。 If it's myclass.class, then I too get null returned from getTransferData, and it throws an IOException. 如果它是myclass.class,那么我也从getTransferData返回null,并抛出IOException。 I have no idea why this occurs. 我不知道为什么会这样。 It took me several hours to determine this. 我花了几个小时才确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM