1

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

enter image description here

How to fix it then?

0

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Because you didn't add your nodes into the root one
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.