Reputation: 56199
I have TextArea in my Java app and I append lot off text rows. I need to ScrollPane to scroll to last appended ( to bottom of TextArea ). How I can do that ?
Upvotes: 3
Views: 4498
Reputation: 753
You should check, if you are on the EDT, when you call the append. If you are outside of the EDT when calling append, the JTextArea does not scroll to the last appended line (see my post with a runnable example at: JScrollPane scroll at last added line).
Upvotes: 0
Reputation: 29560
As Amjad Masad already said, you need to set the caret position to the last position of the document after inserting your text.
I want to add following note: if you use the JTextArea
as some kind of output log (AKA a working thread fills it continually), I only would set the caret position at the end of the document, if the current caret position is already at the end (before the insertion). This allows the user to click somewhere inside the text and read it without having the application automatically scroll down. If the user wants to see the latest and greatest, well, then (s)he has to press Ctrl+Down to put the caret at the end of the document.
Upvotes: 2
Reputation: 324128
Text Area Scrolling explains how scrolling works in a little more detail and provides an alternative solution which may be easier.
Upvotes: 4
Reputation: 1998
If you scroll to the middle of the page where it talks about the policy, you should find what your trying to achieve: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
Upvotes: 2
Reputation: 4035
You could do that by moving the caret position to the bottom, this would automatically scroll the TextArea:
textArea.setCaretPosition(textArea.getDocument().getLength());
Upvotes: 9