在 HTML 格式的 JEditorPane 中选择指定文本
我正在使用 HTML 格式化文本的 Java JEditorPane 中显示文本。我还设计了一个搜索功能,可以在 JEditorPane 中查找文本,选择文本,然后滚动到它。我的问题是创建一个算法,该算法将实际指定选择的开始和结束位置。
如果我只是使用 myeditorpane.getText() 检索文本,然后在结果中查找搜索字符串,则会选择错误的文本来计算错误的选择开始和结束位置(标签导致计算失败)。我尝试在搜索文本之前通过执行替换所有函数 text.().replaceAll("\<.*?>","") 来删除 html 标签(此替换全部会删除标签之间的所有文本)但仍然计算出错误的选择点(尽管我已经接近:-))。
有谁有一个简单的方法来做到这一点?
谢谢,
埃利奥特
I am displaying text in a Java JEditorPane using HTML to fomrat the text. I am also designing a search function that finds text in the JEditorPane selects the text and then scrolls to it. My problem is creating an algorithim that will actually specify the beginning and ending position for the selection.
If I simply retrieve the text using myeditorpane.getText(), then find the search string in the result, the wrong selection start and end positions are calculated with the wrong text being selected (the tags are throwing the calculation off). I tried removing the html tags by executing a replace all function text.().replaceAll("\<.*?>","") before searching for the text (this replace all removes all text in between the tags) but still the wrong selection points are calculated (although I'm getting close :-)).
Does anyone have an easy way to do this?
Thanks,
Elliott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能希望使用底层 < code>Document,而不是原始文本,如此 突出显示示例。
You probably want to be working with the underlying
Document
, rather than the raw text, as suggested in this HighlightExample.您需要找到文本的起始位置。我想是这样的:
然后滚动你可以使用:
阅读
You need to find the start location of the text. I guess something like:
Then to scroll you can use:
Read up on Text and New Lines for more info.