Java Swing - Using JScrollPane and Having it scroll back to top

Viewed 74491

I'm using JScrollPane to allow scrolling in a JFrame that has a text component that's serving as a text editor. What I want to do, after setting the text in this editor, is have it scroll back up to the top, so you can see what's at the beginning of the file.

Does anyone know how to do this?

7 Answers

You can try this:

 scrollPane.getViewport().setViewPosition(new Point(0,0));

According to the JavaDocs setViewPosition() behaves like this:

Sets the view coordinates that appear in the upper left hand corner of the viewport, does nothing if there's no view.

Related