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

时间:2012-12-05 19:10:13

标签: java swing drag-and-drop jtree

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

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

代码如下:

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个对象。例如。如果从表中选择了三行,则调用getTransferData中的cObjectList方法,该方法正确地返回arraylist中的这些对象,但是当它到达DropTargetContext.java中的getTransferDat a时,数组内的值列表返回为null,甚至认为列表/对象本身仍显示size =3

为树选择的放置模式为:

  

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

任何人都可以帮我弄清楚我做错了吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我遇到了类似的问题,结果发现DataFlavor构造函数的第一个参数必须是Object.class。如果它是myclass.class,那么我也从getTransferData返回null,并抛出IOException。我不知道为什么会这样。我花了几个小时来确定这一点。