Every time (except first time) I set text in JEditorPane scrollbar jumps to the bottom of JEditorPane. Is there any way to avoid this? One way that I tried is to use setCaretPosition(0) after setText(). It works but it’s not exactly what I need. Scrollbar jumps down and then jumps up. Is there a way to avoid any movement of scrollbar at settext()?
My code:
package test;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class Test extends JApplet implements HyperlinkListener {
JEditorPane dataDisplayer = new JEditorPane();
public void init() {
ataDisplayer.setEditable(false);
dataDisplayer.add(new JButton());
dataDisplayer.setContentType("text/html");
JScrollPane jsp = new JScrollPane(dataDisplayer);
dataDisplayer.setText("<a href=''>Change Text</a><br><br><br><br><br><br><br><br><br><br>bla");
this.dataDisplayer.addHyperlinkListener(this);
setContentPane(jsp);
}
@Override
public void hyperlinkUpdate(HyperlinkEvent arg0) {
dataDisplayer.setText("<a href=''>Change Text</a><br><br><br><br><br><br><br><br><br><br>bla bla");
}
}
There are different ways to go with that. One would be to simply avoid caret visibility adjustement, like this: