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");
}