I'm working on an applet that loads the nodes of a JTree from a database. When a node is selected, its data are displayed in a different panel. When a "reload" button is clicked, all the nodes in the JTree are removed except for the root node, and the JTree is recreated from the database. I'm try to get the new JTree to expand and select the rows that were selected before reloading, but I can't get this to work.
Before removing the nodes, I save the currently selected row numbers using JTree.getSelectionRows(). After recreating the JTree, I re-select the previously selected rows and try to expand them:
// tree is the name of the JTree.
tree.setSelectionRows(selected);
for (int i= 0; i < Array.getLength(selected); i++) {
System.out.println("selected row: " + selected);
tree.scrollRowToVisible(selected[i]);
}
tree.updateUI();
The previously selected rows do not automatically become visible in the resulting JTree. I also tried using tree.expandRow instead of scrollRowToVisible, with the same results.
Any help would be appreciated!