jtree transferhandler,数据未正确插入

时间:2013-02-24 19:59:08

标签: java drag-and-drop jtree

我尝试将组件拖到jtree上。

对某些案例不起作用:

root

这是一片叶子 B是A的孩子

如果我尝试像B的孩子一样插入它,它将像根的子项一样插入        公共类TreeTransferHandler扩展了TransferHandler {

JTree tree;
DataFlavor nodesFlavor;
DataFlavor[] flavors = new DataFlavor[1];
DefaultMutableTreeNode[] nodesToRemove;
String mimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=" + CommandBeans.class.getName();
DataFlavor dataFlavor = new DataFlavor(mimeType, "command");

public TreeTransferHandler(JTree tree) {
    this.tree = tree;
}

public TreeTransferHandler() {
}

@Override
public boolean canImport(TransferHandler.TransferSupport info) {

    if (!info.isDataFlavorSupported(dataFlavor)) {
        return false;
    }

    return true;
}

@Override
public int getSourceActions(JComponent c) {
    //Nous n'autorisons donc que la copie ici
    return COPY;
}

@Override
public boolean importData(TransferHandler.TransferSupport support) {

    if (!canImport(support)) {
        return false;
    }


    JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();

    TreePath path = dl.getPath();
    int index = dl.getChildIndex();
    CommandBeans command = null;

    Transferable data = support.getTransferable();
    try {
        command = (CommandBeans) data.getTransferData(dataFlavor);
    } catch (UnsupportedFlavorException ex) {
        Logger.getLogger(TreeTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(TreeTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(command.getName());

    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();

    DefaultTreeModel model = (DefaultTreeModel) this.tree.getModel();
    index = (index == -1) ? model.getChildCount(path.getLastPathComponent()) : index;
    model.insertNodeInto(newNode, parentNode, index);

    tree.makeVisible(path.pathByAddingChild(newNode));
    tree.scrollPathToVisible(path);

    return true;
}
}

就像掉落位置和插入一样不好。

似乎有一个错误:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6560955#

有什么建议吗?

0 个答案:

没有答案