updating JTree in java GUI

sajad picture sajad · Sep 29, 2010 · Viewed 15.1k times · Source

I used a JTree in my GUI and added it to a JFrame. When I want to update it and change it's nodes in another part of my program (while program is running, as an action performed) I try to add new nodes, or remove nodes to it; But my interface doesn't change. Please suggest me a solution.

regards

Answer

camickr picture camickr · Sep 29, 2010

In addition to the insertNodeInto suggestion you can also use:

DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
root.add(new DefaultMutableTreeNode("another_child"));
model.reload(root);