This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTree - How To Display Non-Root Node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree - How To Display Non-Root Node" Watch "JTree - How To Display Non-Root Node" New topic
Author

JTree - How To Display Non-Root Node

Patrick Becker
Greenhorn

Joined: Jan 20, 2012
Posts: 4
Hi Folks,

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?

Thanks in advance!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Welcome to the Ranch!

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Patrick Becker
Greenhorn

Joined: Jan 20, 2012
Posts: 4
Perfect! Thanks for your help!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome
Patrick Becker
Greenhorn

Joined: Jan 20, 2012
Posts: 4
Rob Spoor wrote:Welcome to the Ranch!

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
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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."
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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
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.

Here's what my code looks like:

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

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...


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
frank fera
Greenhorn

Joined: Nov 03, 2012
Posts: 2
This is a simple question, with no visible solution. Is it a bug in java's scrollPathToVisible ???

If you are interested in scrolling your tree to a particular node, using scrollPathToVisible, it seems like common sense to me that you'd want that particular tree node at the top of the display.
What is the possible use case that would justify positioning such a tree node at the bottom of your display?

???

I'm going to investigate this further because I only just hit this problem, noticed this forum entry and was surprised that it didn't go further.
frank fera
Greenhorn

Joined: Nov 03, 2012
Posts: 2
here's what I found, thanks goes to the kind person who responded with this solution (elsewhere)
...

As already noted: all scrollXXToVisible methods scroll such that the given XX is visible somewhere, they don't support finer control as f.i. "should be first node in visible area". You have to implement that functionality yourself, something like
TreePath path = tree.getSelectionPath();
if (path == null) return;
Rectangle bounds = tree.getPathBounds(path);
// set the height to the visible height to force the node to top
bounds.height = tree.getVisibleRect().height;
tree.scrollRectToVisible(bounds);

...
 
 
subject: JTree - How To Display Non-Root Node
 
Similar Threads
JTree and JEditorPane
JTree
JTree
JTree
Questions about JTree