在 JEditor 窗格中设置插入符号会导致异常

发布于 2024-08-26 19:39:25 字数 342 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诺曦 2024-09-02 19:39:25
int len = editorPane.getText().length();

为您提供文本和标签的长度。

尝试使用:

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

它只会给出文档中文本的长度。

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.

行至春深 2024-09-02 19:39:25

我认为问题在于文本不仅包含可渲染字符(文本中有 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 call editorPane.setCaretPosition(2), I think that the caret will be between '2' and '3';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文