【问题标题】:Iterating through JEditorPane遍历 JEditorPane
【发布时间】:2017-05-16 02:26:53
【问题描述】:

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

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

【问题讨论】:

    标签: java swing document jeditorpane


    【解决方案1】:

    扩展@Mad 的answer,如果您的DocumentHTMLDocument,您可以使用ElementIterator 进行探索,如图here

    【讨论】:

      【解决方案2】:

      我能想到的最简单的方法就是浏览文档。

      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 都使用相似的方法来实现不同的目标

      【讨论】:

      • 谢谢,这对我很有帮助。
      • 你可以看看thisthis使用了类似的技术
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2018-07-15
      • 2014-01-23
      • 2018-11-03
      • 2015-08-11
      相关资源
      最近更新 更多