Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling scrolling to end of text in JEditorPane

Hi
I used a JEditorPane with HTMLEditorKit to showing HTML text with ability to wrap text.
The problem is when I set it's content using .setText method it automatically scrolls to the end of that text.
How can I disable this?

Thanks.

like image 929
Ariyan Avatar asked Mar 18 '11 10:03

Ariyan


1 Answers

You can try this trick to save the cursor position before the setText() and then restore it once you've added your text to the component:

int caretPosition = yourComponent.getCaretPosition();
yourComponent.setText(" your long text  ");
yourComponent.setCaretPosition(Math.min(caretPosition, text.length()));
like image 84
javanna Avatar answered Oct 20 '22 22:10

javanna