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

时间:2011-03-18 10:31:47

标签: java swing scroll jeditorpane htmleditorkit

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

感谢。

3 个答案:

答案 0 :(得分:5)

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

int caretPosition = yourComponent.getCaretPosition();
yourComponent.setText(" your long text  ");
yourComponent.setCaretPosition(Math.min(caretPosition, text.length()));

答案 1 :(得分:3)

试试这个:

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

答案 2 :(得分:1)

setText

之后尝试此操作
Rectangle r = modelToView(0); //scroll to position 0, i.e. top
if (r != null) {
  Rectangle vis = getVisibleRect(); //to get the actual height
  r.height = vis.height;
  scrollRectToVisible(r);
}
相关问题