Hi all, Before you think I have been lazy and not done a search on previous posts, believe me I have, but still did not find a solution to my problem...or at least not one that I understand Anyway, here it is: I am using a JTree which is built from a database table. The database can be updated by other users so I need to rebuild the tree periodically or when the user selects refresh. At the moment (in the legacy code ) there is a class MyTree which extends from JTree also a class MyTreeModel which implements the TreeModel interface. At the moment when the user selects refresh, a new MyTreeModel is created using the data from the database, and setModel is invoked on the tree to give it the new MyTreeModel object. Then finally all the TreeModelListeners are informed by invoking treeStructureChanged. My problem is this: In the current implementation when the user selects refresh the entire tree collapses. I want to be able to restore the tree to the way it was before refresh was pressed. I am getting a list of the expanded paths (getExpandedDescendants) and the selected node (getSelectionPath) before setModel is called for the new MyTreeModel. That bit is fine. I then try to expand these paths and set the same selection (expandPath, setSelectionPath) on the tree with the new model. It seems to work fine, ie none of the calls throws an exception, BUT the tree still collapses... Do I need to do something else to make the expanded paths and new selection visible somehow??? I have tried scrollPathToVisible and a couple of other things but nothing seems to work. Can anybody please help, Thanks in advance Alex
Hi all, I have got a little further...instead of creating a new model I am now just reloading the model, removing any nodes that have been deleted in the database and adding any that have been inserted. I get the paths before the reload and they print out fine. I get the selection before reload and it also print out what it should. Then I set them again after the reload, and call updateUI() on the tree. This time some of the paths are expanded on the GUI and sometimes the selection is selected...but not always, and there are always some paths which weren't expanded (usually paths more than 2 levels deep). I am totally at a loss , can anyone shed any light on what I'm doing wrong? Please! Here is a snippet of my code
Just a thought on your problem, don't know if it could solve your problem or not. Correct me if I am wrong, anyone. Let say:
When you remove C, does the path to E change? You said some paths are not expanded. Is any node along that path removed or added? If those non-expanded paths always have a removed/added node, then the problem, I guess, will be the previous expanded path no longer valid. Same applies to the selection. Anyone, please correct me if I am wrong. Thanks, kawaii [ March 07, 2002: Message edited by: kawaii desu ]
Originally posted by kawaii desu: When you remove C, does the path to E change? You said some paths are not expanded. Is any node along that path removed or added? If those non-expanded paths always have a removed/added node, then the problem, I guess, will be the previous expanded path no longer valid. Same applies to the selection.
Yes Kawaii I think you are right. It is the paths which are modified which are not expanded or selected. And also if I use getRowForPath() just before I call expandPath, some return -1, so I guess that proves that the path is no longer valid. Do you know of some way I could update the paths as nodes are added or removed or maybe you have an idea for a better way to implement the refresh? Basically I want the refresh to work just like in File Explorer...Thanks Alex