简体   繁体   中英

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. 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 .

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. 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. 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 .

The drop mode selected for the tree is:

treeObjectStructure.setTransferHandler(new ObjectTreeTransferHandler()); 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. If it's myclass.class, then I too get null returned from getTransferData, and it throws an IOException. I have no idea why this occurs. It took me several hours to determine this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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