Sheldon
Sheldon

Reputation: 276

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);
        }
    });
}

Upvotes: 0

Views: 285

Answers (2)

mbw
mbw

Reputation: 366

I think I've done something similar to what you're looking for. Take a look at my answer to this question here:

Keep JScrollPane as same length java

Upvotes: 0

StanislavL
StanislavL

Reputation: 57421

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

You can try update policy for the caret

Upvotes: 1

Related Questions