Go Go Gnome
the website of Sander Kooijmans

JViewPort.scrollRectToVisible() does not work as expected

Posted: December 15, 2011

Scrolling a JScrollPane such that a component becomes visible should work like this:

Rectangle bounds = component.getBounds();
scrollPane.getViewport().scrollRectToVisible(bounds);

Unfortunately, the code above does not work. The following code does the trick:

Rectangle bounds = component.getBounds();
Point p = scrollPane.getViewport().getViewPosition();
bounds.translate(-p.x, -p.y);
scrollPane.getViewport().scrollRectToVisible(bounds);

I submitted a bug in Sun’s bug database: see bug 7077308 in the bug database.

Update May 2, 2015: The bug is not present anymore in the bug database.