1

我尝试将组件拖到 jtree 上。

这不适用于某些案例示例:

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

如果我尝试像 B 的孩子一样插入它,它将像根公共类 TreeTransferHandler 的孩子一样插入

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#

有什么建议吗?

4

0 回答 0