I'm new to the forum and I think I have a somewhat elementary question, but I can't find information on it.
I created a JTree with a DefaultTreeModel with the top level directory "/" defined as the root node. How can I display (have focus on / expand) a specific subdirectory other than the root node when the JTree is first started up?
JTree has method scrollPathToVisible(TreePath). This will not only scroll to that TreePath, but also expand all of its ancestors to make sure it's shown. Now all you need to do is create the right TreePath. If you use DefaultMutableTreeNode that's easy - just create a new TreePath around the value returned by the node's getPath() method. Otherwise you will have to construct it differently.
Once it's visible, you can call setSelectionPath(TreePath) to also select the node.
JTree has method scrollPathToVisible(TreePath). This will not only scroll to that TreePath, but also expand all of its ancestors to make sure it's shown. Now all you need to do is create the right TreePath. If you use DefaultMutableTreeNode that's easy - just create a new TreePath around the value returned by the node's getPath() method. Otherwise you will have to construct it differently.
Once it's visible, you can call setSelectionPath(TreePath) to also select the node.
Just one more question. When I use setSelectionPath(TreePath) to select a node, it correctly highlights that directory, but it places it at the bottom of the viewport where the contents of the expanded directory cannot be viewed. Is there a way for the expanded/selected node (directory) to be placed at the top of the viewport window so that the contents of the node can be viewable? I've tried using setScrollsOnExpand(true), but that doesn't seem to do it.
Michael Dunn
Rancher
Joined: Jun 09, 2003
Posts: 4041
posted
0
did you try Rob's first suggestion?
"JTree has method scrollPathToVisible(TreePath). This will not only scroll to that TreePath, but also expand all of its ancestors to make sure it's shown."
I think that just makes sure that the node is made visible. I doubt that it matters whether that's on the top or bottom.
One trick could be to collapse and expand the node using collapsePath and expandPath. That should display as many of the children as possible.
Patrick Becker
Greenhorn
Joined: Jan 20, 2012
Posts: 4
posted
0
Rob Spoor wrote:I think that just makes sure that the node is made visible. I doubt that it matters whether that's on the top or bottom.
One trick could be to collapse and expand the node using collapsePath and expandPath. That should display as many of the children as possible.
I just tried that, but it didn't resolve the problem. The highlighted directory is still being placed at the bottom of the viewport, thus hiding the directories expanded contents.
Interestingly enough, when I manually collapse and then expand the directory via the UI, it behaves as I would expect it to - the expanded node is placed at the top of the viewport, with the directory contents displayed below it.
Patrick Becker wrote:Interestingly enough, when I manually collapse and then expand the directory via the UI, it behaves as I would expect it to - the expanded node is placed at the top of the viewport, with the directory contents displayed below it.
But, in this case, is the selected child viewable?
What I am trying to ask is:
Suppose, the selected child is down in the hierarchy with say 5 levels. And your JTree has a height where only 3 or 4 nodes can be seen at any time, then, if the topmost parent is the first viewable row, then the last child won't be shown. Same way, when the child is shown (as happens when you use the first idea of this post), then, only upto 3 levels in the hierarchy will be viewable.
Bascially, this might boil down to a preferred height of the tree or layout issue...