I've got a JTree that's a file browser. At startup, the selection path is set to the user's home directory. This tree is in a JScrollpane. What I'd like to do is to set the vertical scroll bar position to have this cell (the user's home dir.) on top of the viewable area.

Basically, I need to find out the position of the selected cell, then move the scrollbar to where it will be visible at the top of the panel. Anyone know how to do this?

Any help is greatly appreciated, Thanks, Bryant P.

10 Years
Discussion Span
Last Post by bryantpurdin

I assume you have already tried scrollPathToVisible (TreePath path), which will ensure that the path is visible, but not necessarily at the top. You will probably have to play a bit with scrollRectToVisible (Rectangle aRect) and creating a Rectangle that produces the desired result. Beyond that you could work directly with the JScrollPane and JViewport to position it at a desired coordinate. That will take a bit more experimentation with the various relative dimensions of the selected node and the containers.

Edited by Dani: Formatting fixed


Actually, I didn't know about the call to scrollPathToVisible(), I was too busy looking at the JScrollpane. I'll just settle for it being visible, and if they don't like that, then I'll look into the Rectangle call.

Thanks, Bryant P.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.