简体   繁体   中英

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:

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

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