John
John

Reputation: 1596

Moving JTextArea Scroll to Caret

How can I move the current scroll view of a JTextArea down so that the caret appears at the top of the JTextArea? Thanks.

Upvotes: 0

Views: 1476

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109557

You can do:

Point pt = textArea.getCaret().getMagicCaretPosition();
Rectangle rect = new Rect(pt, new Dimension(1, 10));
textArea.scrollRectToVisible(rect);

One can also use the getDocument for a better selection.

Upvotes: 1

Jonathan
Jonathan

Reputation: 7391

Place the JTextArea inside a JScrollPane, and invoke

scrollPane.getVerticalScrollBar().setValue(value);

Upvotes: 0

Related Questions