[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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?
0
chenwei
Asked:
chenwei
  • 4
  • 2
  • 2
  • +1
1 Solution
 
girionisCommented:
Try:

textArea.setCaretPosition( textArea.getText().length() );
0
 
tomboshellCommented:
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.
0
 
girionisCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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.
0
 
chenweiAuthor Commented:
thanks to the info from all sites. Really no any better way than using setCaretPosition()?
0
 
girionisCommented:
I can't think of anything else :(
0
 
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 ;-)
0
 
girionisCommented:
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.
0
 
tomboshellCommented:
s'ok, everything else was reiteration or further thoughts/suggestions
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now