I'm serializing my JTree expansion state in xml. When my app starts up
and rebuilds the tree, I want to expand any nodes that were visible when
the app last closed. Here is a snippet of code that is supposed to do this:
DefaultMutableTreeNode childTreeNode = new
DefaultMutableTreeNode();
int index = treeNode.getChildCount();
( ( DefaultTreeModel ) getNavigationTree().getModel()
).insertNodeInto( childTreeNode, treeNode, index );
.........
Attr visibilityAttribute = nodeElement.getAttributeNode(
"visible" );
if ( visibilityAttribute != null )
{
if ( visibilityAttribute.getValue().equals( "true" ) )
{
DefaultTreeModel model = ( DefaultTreeModel )
getNavigationTree().getModel();
final TreePath path = new TreePath( model.getPathToRoot(
childTreeNode ) );
Runnable makeVisibleRunnable = new Runnable() { public
void run(){ getNavigationTree().makeVisible( path ); } };
SwingUtilities.invokeLater( makeVisibleRunnable );
}
}
I use invokeLater because this code is being run outside of the event
dispatch thread.
The problem is that the code has no effect. The tree always comes up in
a collapsed state.
Any ideas?
Thanks,
Dave Neuendorf
I found the problem myself. Elsewhere in my code, after the tree had
been reconstructed, its model.reload() was being called. This, of
course, was collapsing the tree.
Thanks to anyone who was thinking about this for me.
Dave Neuendorf
> I'm serializing my JTree expansion state in xml. When my app starts up
> and rebuilds the tree, I want to expand any nodes that were visible
[quoted text clipped - 33 lines]
> Thanks,
> Dave Neuendorf
Andrew Thompson - 02 Nov 2005 18:40 GMT
...
> Thanks to anyone who was thinking about this for me.
(as one of those people) No worries, you're welcome.
Thanks for having the sense to report the nature of the fix.