This is how I scroll all the way up or down automatically:
/**
* Scrolls a {@code scrollPane} all the way up or down.
*
* @param scrollPane the scrollPane that we want to scroll up or down
* @param direction we scroll up if this is {@link ScrollDirection#UP},
* or down if it's {@link ScrollDirection#DOWN}
*/
public static void scroll(JScrollPane scrollPane, ScrollDirection direction) {
JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
// If we want to scroll to the top, set this value to the minimum,
// else to the maximum
int topOrBottom = direction == ScrollDirection.UP ?
verticalBar.getMinimum() :
verticalBar.getMaximum();
AdjustmentListener scroller = new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
Adjustable adjustable = e.getAdjustable();
adjustable.setValue(topOrBottom);
// We have to remove the listener, otherwise the
// user would be unable to scroll afterwards
verticalBar.removeAdjustmentListener(this);
}
};
verticalBar.addAdjustmentListener(scroller);
}
public enum ScrollDirection {
UP, DOWN
}