CodeBlue
CodeBlue

Reputation: 15399

How to prevent JScrollPane from automatically scrolling down?

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.

Upvotes: 2

Views: 1915

Answers (1)

el_stack
el_stack

Reputation: 538

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));

Upvotes: 4

Related Questions