Search code examples
javaswingscrolljeditorpanehtmleditorkit

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.


Solution

  • 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()));