Refreshing JTree without using DefaultTreeModel

Posted on 2004-03-21
Last Modified: 2007-12-19
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".
      // The selection path will now be set to rootPath.

      // Restore the old selection path.

But this will fire unwanted TreeSelectionEvents, so I would prefer another solution.
Question by:xelia
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 92

Expert Comment

ID: 10647090
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.

Author Comment

ID: 10655309
Thanks objects,

I'll ask the people who created the tree model if they are firing the event when the node's data changes.

Author Comment

ID: 10673233
Unfortunately their TreeModel is not firing events when the nodes' contents changes, and I don't have access to the source code.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 10673375
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.
LVL 92

Accepted Solution

objects earned 300 total points
ID: 10674047
> It would be nice to have a utility method to refresh any JTree

There is: setModel().

Author Comment

ID: 10674349
This works, but like you were saying before, the selected path gets lost.  :(
LVL 92

Expert Comment

ID: 10674364
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.

Author Comment

ID: 10674437
Thanks objects - that answered my next question (why changing the model was firing TreeSelectionEvents).
LVL 92

Expert Comment

ID: 10700752

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 261
spring jars download 1 65
Java array 21 139
Selenium findElement(By.classname  identifier 15 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question