首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历JEditorPane

遍历JEditorPane
EN

Stack Overflow用户
提问于 2012-11-22 09:46:38
回答 2查看 257关注 0票数 0

JEditorPane中,我想搜索一个字符串,我知道scrollToReference不起作用,所以我想知道如何遍历窗格并找到指定的字符串。

如何遍历JEditorPane以在java中查找指定的字符串?

EN

回答 2

Stack Overflow用户

发布于 2012-11-22 09:51:34

我能想到的最简单的方法就是遍历文档。

代码语言:javascript
复制
Document document = editor.getDocument();
try {
    String find = //... String to find
    for (int index = 0; index + find.length() < document.getLength(); index++) {
        String match = document.getText(index, find.length());
        if (find.equals(match)) {
            // You found me
        }
    }
} catch (BadLocationException ex) {
    ex.printStackTrace();
}

您可以根据这个概念编写一些例程来“查找下一个单词”

您还可以找到Highlight a word in JEditorPaneJava - Scroll to specific text inside JTextArea,它们都使用类似的方法来实现不同的功能

票数 4
EN

Stack Overflow用户

发布于 2012-11-22 09:55:39

在@Mad的answer上展开,如果你的Document是一个HTMLDocument,你可以使用ElementIterator来探索它,如here所示。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13504791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档