Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reload JTree and keep expanded nodes

843806Aug 17 2007 — edited Sep 2 2007
Hi all,

I have spent some time looking around on this, found some useful stuff, but I still cannot seem to get a result.

Here is my situation:

I have a JTree for a GUI desktop app. I build the tree from a database file. The user has the ability to refresh the tree to keep it in sync with any database changes. The problem is that when I refresh, all the nodes that were previously expanded are now collapsed again.

Having looked around, I have seen a lot of JTree.expandRow() answers, but I don't want to use expandRow, as the rows will changes according to any db changes.

I thought a good solution would be to trap any expand or collapse events and for each either add or remove the TreePath for the relevant node onto a List. When the users refreshes the tree, I thought I could run through the list and expand any TreeNodes I find. I also found someone advocating such an approach in another thread. BUT - it doesn't work for me Here is the code for my refresh. Any help/advice, greatly appreciated.
Thanks in advance,
Steve
private void refreshBrowser(){
        
        System.out.println("Refresh browser - Started");        
        
        rootTreeNode.removeAllChildren(); // remove all nodes from root
        loadBrowserData(); // This method loads nodes from a database
        
         Iterator<TreePath> TPI = objectTreePaths.iterator(); // run throught my saved paths
        while (TPI.hasNext()) {
            TreePath yTreePath = TPI.next();            
            browserTree.expandPath(yTreePath); // Expand each found path
        }
         
        treeModel.reload(); // model changed - reload it.
        
        System.out.println("Refresh browser - Complete");        
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 30 2007
Added on Aug 17 2007
8 comments
966 views