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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.