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