开发者

Disabling scrolling to end of text in JEditorPane

Hi

I used a JEditorPan开发者_JAVA技巧e 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.


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


Try this:

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


Try this after 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜