I am testing some code with JTree
, and I noticed that model.reload()
gives different output than model.nodeChanged(currentNode)
. The correct output is obtained from model.reload()
. Here are my two outputs for the same code:
My code is as follows:
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();
try {
Thread.sleep(1000 * 5);
} catch (Exception e) {
}
t.add_new_folder("Data", "trial 0");
t.add_new_folder("Data/trial 0", "trial 1");
t.add_new_folder("Data/trial 0/trial 1", "trial 2");
t.add_new_folder("Data", "trial 1");
try {
Thread.sleep(1000 * 5);
} catch (Exception e) {
}
t.add_new_folder("Data/trial 1", "trial 2");
t.add_new_folder("Data", "trial 2");
}
public test() {
frame = new JFrame("using reload");
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();
//model.nodeChanged(currentNode);
}
}
I need also to use model.nodeChanged()
as it keeps expanded paths the same unlike model.reload
. Any explanation and how to fix?