简体   繁体   English

在JEditor窗格中设置尖号会导致异常

[英]Setting the a caret in an JEditor Pane causes an exception

I have a JeditorPane which has some text in HTML format. 我有一个JeditorPane,其中有一些HTML格式的文本。 When I execute the following command 当我执行以下命令时

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

The value for len is 7473. But then I try to do the following: len的值是7473。但是然后我尝试执行以下操作:

editorPane.setCaretPosition(4995); editorPane.setCaretPosition(4995);

And I get the following exception: java.lang.IllegalArgumentException: bad position: 4995 而且我得到以下异常:java.lang.IllegalArgumentException:错误位置: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. 我的理解是,仅当我尝试设置插入符号的位置小于0或大于文本长度时,才应获得此异常。 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). 我认为问题在于文本不仅包含可渲染的字符(文本中具有HTML标记)。 I guess that setCaretPosition want's to get visible text position. 我猜想setCaretPosition想要获得可见的文本位置。

For example, if text = "<b>123</b>" and you call editorPane.setCaretPosition(2) , I think that the caret will be between '2' and '3'; 例如,如果text = "<b>123</b>"并调用editorPane.setCaretPosition(2) ,则我认为插入符号将在'2'和'3'之间;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM