How to get to the end of TextArea

Possible duplicate:
How to install AUTO-SCROLLING JTextArea in Java GUI?

I am creating an application in which the user enters text in TextField, from which the text is added to TextAreawhen actionPerformed.
TextAreaadded on JScrollPane. When the number of rows from the declared column, the user can scroll to see the text. But he needs to scroll every time he enters text TextAreathrough TextField, because the last line added to TextAreadoes not automatically scroll to the last line.
Can someone help me there, either automatically or when actionPerformed, the scrolling TextAreawill be the last?

+5
source share
2 answers

Try the following:

jTextArea.selectAll();
int last = jTextArea.getSelectionEnd();
jTextArea.select(last, last);

Where jTextAreais the link to your TextArea.

However, the previous example can be very slow if there is a lot of text, so I provided another way to do this:

jTextArea.setCaretPosition(jTextArea.getDocument().getLength());

EDIT: After browsing the internet for alternative solutions and reading this answer to a similar question, I realized that @kleopatra's solution is more efficient. Nevertheless, it is entirely your prerogative to accept any answer that you consider necessary (I see you accepted mine).

@kleopatra I supported you to compensate. :)

+3
source

The magic is in DefaultCaret.updatePolicy:

DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

. Rob

+5

All Articles