File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTree reload question, AGAIN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree reload question, AGAIN" Watch "JTree reload question, AGAIN" New topic

JTree reload question, AGAIN

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Ok, this question has been asked mutliple times in different varients and forms. So here goes my version because after searching Javaranch and google for 2 days, I can't seem to find a valid answer.
I have a JTree. I build the Tree through a DefaultTreeModel which I have over-ridden. All my values in the Tree come from a databse. Each leaf node is a CustomObject I have created which holds all the relevent information about the leaf object. That's all fine and dandy.
Now, there are various operations that can be performed on the tree leaf objects. Any one of these operations causes the database information to change and the location of the leaf on the tree to change.
Since this app runs on several PC's, the easiest way for me to pick up all the new data is to reload the Tree Model. The problem is that this collapses my Tree. I need the tree to stay expanded to the degree that the user had it before changing information. But the tree also needs to reflect to moves and changes of the leaf nodes.
I tried using a TreePath to store the expanded state of the Tree, but it saves the expanded state with the leaf nodes in their original places instead of them being moved.
Any ideas, suggestions, or comments are welcome to help me solve my problem.

GenRocket - Experts at Building Test Data
Eric Blische

Joined: Feb 02, 2003
Posts: 16
One possible approach is to get all the leaf nodes (which are DefaultMutableTreeNodes ?), and throw the buggers into a vector. On second thought, get each of their TreePath's and throw THEM into a vector, instead. Then once the model is loaded, pull the TreePaths outta the vector and iteratively expand each one (using getLastPathComponent I think?).
Hope this helps, it's not pretty but might get you functional until a better solution can be investigated ....
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Thanks for the reply. I have actually been playing around with use the Observable and Observer classes. I think my model is different from most, because I not just adding and removing nodes and I NEVER have to change the name of the node itself. I use a custom object for my leaf nodes, and the properties of that object is what changes and sometimes, the location of that node in the tree. But it never gets removed from the tree all together. So that is where I am having problems.
I agree. Here's the link:
subject: JTree reload question, AGAIN
It's not a secret anymore!