How do I get a JScrollPane to automatically scroll to the bottom of a document each time a new text is added to the document?

Jesper Berglund

When a text is insterted in a JTextPane, the height of the JTextPane will increase, but the visible rect of the scrollpane is constant. To keep the lowest row visible use the scrollRectToVisible() function and specify where the scrollpane shall scroll.

Example:

jTextPane1.insertString(
  jTextPane1.getDocument().getLength(),
  intsertString +"
", null);

//scroll the text as it gets more and more
jTextPane1.scrollRectToVisible(
  new Rectangle(0,jTextPane1.getHeight()-2,1,1));
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact