javaswingjtree

How to update JTree elements


I use JTree with TreeNode which extending DefaultMutableTreeNode.when I add new node,i cant update JTree.Any help will be appreciated


Solution

  • Adding to the node only is not enough, the Tree needs to be notified. The correct way to do this is to let the model take care of adding/removing nodes, as it will notify correctly the Tree on the way.

    In case of a DefaultTreeModel (which should be the one used in the JTree if you haven't made your own TreeModel), you have the method insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index), which you can use.