• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree reload question, AGAIN

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks.
 
Eric Blische
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya,
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 ....
Eric
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic