开发者

Setting the a caret in an JEditor Pane causes an exception

I have a JeditorPane which has some text in HTML format. When I execute the following command

int len = editorPane.getText().length();

The value for len开发者_运维百科 is 7473. But then I try to do the following:

editorPane.setCaretPosition(4995);

And I get the following exception: java.lang.IllegalArgumentException: bad position: 4995

My understanding is that I should only get this exception if the position at which I'm trying to set the caret is less than 0 or greater than the text length. It is neither. How can this be.

Thank you,

Elliott


int len = editorPane.getText().length();

Gives you the length of the text and the tags.

Try using:

int len = editorPane.getDocument().getLength();

which will only give you the length of the text in the document.


I think that the problem is that the text does not only contains renderable characters (you have HTML tag in the text). I guess that setCaretPosition want's to get visible text position.

For example, if text = "<b>123</b>" and you call editorPane.setCaretPosition(2), I think that the caret will be between '2' and '3';

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜