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?
