Search code examples
javaswingscrolljtextarea

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.


Solution

  • 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.