Solved

Refreshing JTree without using DefaultTreeModel

Posted on 2004-03-21
9
855 Views
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".
      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
Comment
Question by:xelia
  • 5
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
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.
0
 

Author Comment

by:xelia
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.
0
 

Author Comment

by:xelia
ID: 10673233
Unfortunately their TreeModel is not firing events when the nodes' contents changes, and I don't have access to the source code.
0
 

Author Comment

by:xelia
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.
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Accepted Solution

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

There is: setModel().
0
 

Author Comment

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

Expert Comment

by:objects
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.
0
 

Author Comment

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

Expert Comment

by:objects
ID: 10700752
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now