Take the 2-minute tour ×

I am using a JScrollPane which contains inside it a JTextArea. The problem is when the JTextArea is populated with a lot of text, the JScrollPane automatically scrolls down. What property should I set on the JScrollPane to avoid this automatic scrolling down? Thanks.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

If the JScrollPane is decorating a text component, it will automatically scroll to the bottom, but after it loads calling setCaretPosition(0) on your text component will cause it to scroll to the top.

However, if it's not a text component, you can also alter the viewport like this:

scrollPane.getViewport().setViewPosition(new Point(0,0));
share|improve this answer
Great answer! Is there no way this can be achieved by setting a property on the JScrollPane itself? –  CodeBlue Apr 9 '12 at 16:00
Thanks for updating the answer with the info that I needed. –  CodeBlue Apr 9 '12 at 19:48

Your Answer


