Link to home
Start Free TrialLog in
Avatar of chenwei
chenwei

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tomboshell
tomboshell

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.
>> 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.
Avatar of chenwei

ASKER

thanks to the info from all sites. Really no any better way than using setCaretPosition()?
I can't think of anything else :(
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