禁用在JEditorPane中滚动到文本结尾

RYN*_*RYN 4 java swing scroll jeditorpane htmleditorkit

您好
我使用带有HTMLEditorKit的JEditorPane来显示能够包装文本的HTML文本.
问题是,当我使用.setText方法设置内容时,它会自动滚动到该文本的末尾.
我怎么能禁用它?

谢谢.

jav*_*nna 5

您可以尝试这个技巧来保存光标位置setText(),然后在将文本添加到组件后将其恢复:

int caretPosition = yourComponent.getCaretPosition();
yourComponent.setText(" your long text  ");
yourComponent.setCaretPosition(Math.min(caretPosition, text.length()));
Run Code Online (Sandbox Code Playgroud)


haf*_*ues 5

尝试这个:

final DefaultCaret caret = (DefaultCaret) yourEditorPane.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
//!!!!text must be set AFTER update policy has been set!!!!!
yourEditorPane.setText(text);
Run Code Online (Sandbox Code Playgroud)