Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 912
  • Last Modified:

Refreshing JTree without using DefaultTreeModel

I need to dynamically change the display information of tree nodes (e.g. node text), and then refresh the entire JTree so that my changes are displayed.

DefaultTreeModel has lots of useful methods that I could use to refresh the tree (e.g. reload()), however, there is no guarantee that the JTree will be using DefaultTreeModel.

Is there a way to refresh the tree without using DefaultTreeModel?

I am temporarily using the following code for the refresh:
    if(tree.isExpanded(rootPath)) {
      // Note the currently selected path so that it can be
      // restored after the tree is refreshed.
      TreePath selectedPath = tree.getSelectionPath();

      // Do the "refresh".
      tree.collapsePath(rootPath);
      tree.expandPath(rootPath);
      // The selection path will now be set to rootPath.

      // Restore the old selection path.
      tree.setSelectionPath(selectedPath);
    }

But this will fire unwanted TreeSelectionEvents, so I would prefer another solution.
0
xelia
Asked:
xelia
  • 5
  • 4
1 Solution
 
objectsCommented:
You have to fire the relevant events from the TreeModel, this does not have to be a DefaultTreeModel, but it needs to come from the model.
Any other solution, such as refreshing the model, would cause you to lose your selection as you have already experienced.
0
 
xeliaAuthor Commented:
Thanks objects,

I'll ask the people who created the tree model if they are firing the event when the node's data changes.
0
 
xeliaAuthor Commented:
Unfortunately their TreeModel is not firing events when the nodes' contents changes, and I don't have access to the source code.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
xeliaAuthor Commented:
It would be nice to have a utility method to refresh any JTree, no matter what its model is doing:

public static void refreshAll(JTree tree) {
  // Do refresh here.
}
0
 
objectsCommented:
> It would be nice to have a utility method to refresh any JTree

There is: setModel().
0
 
xeliaAuthor Commented:
This works, but like you were saying before, the selected path gets lost.  :(
0
 
objectsCommented:
yes, as the tree has no way of knowing whether the underlying model data has changed or not so it has to clear any state such as selection.
0
 
xeliaAuthor Commented:
Thanks objects - that answered my next question (why changing the model was firing TreeSelectionEvents).
0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now