如何使用字符串 JTree 创建节点?

How to create node using string JTree?

为了仅通过知道字符串路径来创建 JTree 并插入新节点,我编写了以下行:

private JFrame frame;
JTree tree;
DefaultMutableTreeNode root;
DefaultTreeModel model;

public static void main(String[] args)
{
    test t = new test();
    t.add_new_folder("Data","trial 0");
    t.add_new_folder("Data/trial 0","trial 1");
    t.frame.revalidate();
    t.frame.repaint();

}
public test()
{
    frame = new JFrame();
    tree = new JTree();
    root = new DefaultMutableTreeNode("Data");
    model = new DefaultTreeModel(root);
    tree.setModel(model);
    frame.getContentPane().add(tree, BorderLayout.WEST);
    frame.setVisible(true);
    frame.setSize(500, 500);
}

直到这里一切看起来都很好并且正在工作但是当调用这个方法时它没有达到预期的结果

    public void add_new_folder(String path,String name){
    String[] data = path.split("/");
    TreePath t = new TreePath(data);
    DefaultMutableTreeNode parent = new DefaultMutableTreeNode(t);
    model.insertNodeInto(new DefaultMutableTreeNode(name), parent, parent.getChildCount());
    model.reload();
}

这就是我得到的

那怎么解决呢?

试试这个代码:

public class Test {
    private JFrame frame;
    JTree tree;
    DefaultMutableTreeNode root;
    DefaultTreeModel model;
    private Map<String, DefaultMutableTreeNode> treeMap;

    public static void main(String[] args)
    {
        Test t = new Test();
        t.add_new_folder("Data","trial 0");
        t.add_new_folder("Data/trial 0","trial 1");
        t.frame.revalidate();
        t.frame.repaint();
    }

    public Test()
    {
        frame = new JFrame();
        tree = new JTree();
        root = new DefaultMutableTreeNode("Data");
        model = new DefaultTreeModel(root);
        tree.setModel(model);
        frame.getContentPane().add(tree, BorderLayout.WEST);
        frame.setVisible(true);
        frame.setSize(500, 500);
        treeMap = new HashMap<>();
        treeMap.put("Data", root);
    }

     public void add_new_folder(String path,String name){
        DefaultMutableTreeNode currentNode = treeMap.get(path);
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(name);
        currentNode.add(childNode);
        treeMap.put(path+"/"+name, childNode);
        model.reload();
    }
}

我知道这是否是您想要的,但它可以成为您跟随的线索 ;)

尝试遍历到特定节点并获取 TreePath。下面的代码应该可以帮到你。

    public void add_new_folder(String path,String name){
        String[] data = path.split("/");

        TreePath tPath = findPath(data[data.length-1]);
        if(tPath != null){
            ((DefaultMutableTreeNode)tPath.getLastPathComponent()).add(new DefaultMutableTreeNode(name));
        }
    }

    @SuppressWarnings("unchecked")
    private TreePath findPath(String s) {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
        Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
        while (e.hasMoreElements()) {
            DefaultMutableTreeNode node = e.nextElement();
            if (node.toString().equalsIgnoreCase(s)) {
                return new TreePath(node.getPath());
            }
        }
        return null;
    }