简体   繁体   English

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

[英]How to create node using string JTree?

In order to create a JTree and inserting new nodes by only knowing string path I wrote the following lines: 为了创建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);
}

till here everything looks good and is working but when this method is called it doesn't do the expected results 直到这里一切看起来都不错并且可以正常工作,但是当调用此方法时,它并没有达到预期的结果

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

and that's what I get 这就是我得到的

在此处输入图片说明

How to fix it then? 那怎么解决呢?

try this code: 试试这个代码:

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

I know if it's what you want, but it can be a lead to follow ;) 我知道这是否是您想要的,但可以带您跟随;)

Try to traverse to particular node and get the TreePath. 尝试遍历特定节点并获取TreePath。 The Below code should help you. 以下代码将为您提供帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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