How to set the vertical Scrollbar of the Scrollpane alwazs at the bottom

I put this question a month ago and got the answer that one can use the function setCaretPosition(). It works. But if the text is getting langer and longer, it takes long time to get the text printed out. I think there must be another betther methor to do that, such as set the scrollbar postion etc.

Here is my question again:
My program has a JScrollPane, it contains a JTextArea. The program will read in text and print it onto the JTextArea. But as the text getting large and longer, the scrollbar moves up-words. I have to use the mause to droll the scrollbar to the bottom so I can see the whole text. How can I set the scrollbar always on the bottom of the textarea as text is printed?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


textArea.setCaretPosition( textArea.getText().length() );

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Depending on the text size it may just take a while, it may be a different problem then what you think it is.  You stated, "it takes long time to get the text printed out"  The text file must be large and large files will take some time.  
Just an idea, display some kind-of status or waiting stuff for the user while it is loading.  Break your processes down:
-Reading the file
-loading the file
-displaying the file.
Once it is displayed then scroll to the last position.  Could implement a splash screen while reading and loading.  

You could also add a DocumentListener to the document contained in the text area and have it react to added text, it is here that you would reposition the cursor.  

Even more complicated would be to introduce multiple threads into the solution.  Main controlling class, threaded file reading class(stores to storage), display classes, temporary storage for the read lines (synchronized, with add and pop methods), threaded class for updating the gui (reads from storage.)  Such a solution, although trickier to implement, could improve the performance.
If you know the text position just pass it in the argument of the setCaretPosition. I guess the delay is in the time it takes to find out the length of the text in the text area.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Mayank SAssociate Director - Product EngineeringCommented:
>> textArea.setCaretPosition( textArea.getText().length() );

- seems to be the best alternative to me. If the file is huge, there is definitely going to be some latency which you cannot evade.
chenweiAuthor Commented:
thanks to the info from all sites. Really no any better way than using setCaretPosition()?
I can't think of anything else :(
Mayank SAssociate Director - Product EngineeringCommented:
Javadoc says (for setText ()):

>> When text has been inserted, the resulting caret location is determined by the implementation of the caret class.

Maybe you can try writing your own Caret class instead of using the DefaultCaret ;-)
I think the only default solution AFAIK si the setCaretPosition which I mentioned in my first post so I'd say I deserve the points.
s'ok, everything else was reiteration or further thoughts/suggestions
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.