Search code examples
javaswingscrolljtextareajtextpane

JTextPane should keeps position at update


I have a long text in a textPane (inside a scrollPane) that updates automatically. After update the pane scrolls to the top of the textPane. How can i deactivate the "top-scroll" (like the textArea does). I can not use a textArea because I style the text in pane with HTML. I can't do this style with a textArea, right? :(

public void create(){
    ...
    scrollpane.setViewportView(textPane);
    textPane.setEditable(false);
    textPane.setContentType("text/html");
    ...
}

public void update(String text){
   textPane.setText(text);
}

Thanks for help!

SOLUTION:

public void update(final String iv_outputText) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            ((DefaultCaret) textPane.getCaret())
                    .setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
            textPane.setText(iv_outputText);
        }
    });
}

Solution

  • ((DefaultCaret)textPane.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE)
    

    You can try update policy for the caret