This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JTree - How To Display Non-Root Node

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Patrick Becker
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect! Thanks for your help!
 
Rob Spoor
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Patrick Becker
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:

 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

...
 
    Bookmark Topic Watch Topic
  • New Topic