• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

JTextPane + JScrollPane problems

Ok, I've Googled all over and can't seem to find a good snippit of code to fix my problem.

All I need is for the ScrollPane to autoscroll to the bottom of the JTextPane everytime some text is appended to it. :frown:

Help please!
 
Although not your specific problem, I've used a JTextArea before to post text messages, which I believe already implicitly contains a vertical scroll bar. I used the JTextArea (as 'area'):

area.append( message + newLine );
area.setCaretPosition( area.getDocument().getLength() );

.. so while I don't know if there is any autoscroll option, you can explciitly call the above method to scroll when required.
 
That code should work for a JTextPane (I've used it before with a custom subclass of JEditorPane and the JTextPane is a subclass of JEditorPane as well). Are you sure you're adding the text pane to the scroll pane correctly?

Timing is important here, since you need to be sure the inner document is actually updated before you can scroll it.

 
Originally posted by: Kilrsat
That code should work for a JTextPane (I've used it before with a custom subclass of JEditorPane and the JTextPane is a subclass of JEditorPane as well). Are you sure you're adding the text pane to the scroll pane correctly?

Timing is important here, since you need to be sure the inner document is actually updated before you can scroll it.

Robot.waitforidle() 😉
 
My mistake Kilsat - It does work 🙂

There was a bug in my code - as I only want the autoscroll to work it the scroll bar is already at the bottom. For some reason after the auto scroll is done that scroll bar is not at the bottom anymore (it is around 1-2 pixcels off).
 
Back
Top