JTextPane + JScrollPane problems

imported_Ned Flanders

Senior member
May 11, 2005
641
0
0
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!
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
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.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
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.

 
Sep 29, 2004
18,656
68
91
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() ;)
 

imported_Ned Flanders

Senior member
May 11, 2005
641
0
0
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).