在 JEditor 窗格中设置插入符号会导致异常
我有一个 JeditorPane,其中有一些 HTML 格式的文本。当我执行以下命令时
int len = editorPane.getText().length();
len 的值为 7473。但是我尝试执行以下操作:
editorPane.setCaretPosition(4995);
我得到以下异常: java.lang.IllegalArgumentException: badposition: 4995
我的理解是,只有当我尝试设置插入符号的位置小于 0 或大于文本长度时,我才应该得到此异常。两者都不是。怎么会这样呢。
谢谢你,
埃利奥特
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为您提供文本和标签的长度。
尝试使用:
它只会给出文档中文本的长度。
Gives you the length of the text and the tags.
Try using:
which will only give you the length of the text in the document.
我认为问题在于文本不仅包含可渲染字符(文本中有 HTML 标签)。我猜想 setCaretPosition 想要获得可见的文本位置。
例如,如果
text = "123"
并且您调用editorPane.setCaretPosition(2)
,我认为插入符号将位于 ' 2'和'3';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 calleditorPane.setCaretPosition(2)
, I think that the caret will be between '2' and '3';