JTree.scrollPathToVisible
is designed to scroll the minimum amount necessary to bring the path into view, so it is probably desirable to use this default behavior. However, assuming you have specific needs to keep the horizontal scrolling constant, you could implement your own version of scrollPathToVisible
:
private void scrollPathToVisible(TreePath treePath) {
if (treePath != null) {
tree.makeVisible(treePath);
Rectangle bounds = tree.getPathBounds(treePath);
if (bounds != null) {
bounds.x = 0;
tree.scrollRectToVisible(bounds);
}
}
}
This is simply a copy of the default implementation with the additional line to set bounds.x
to 0.