【问题标题】:Highlight a word in JEditorPane在 JEditorPane 中突出显示一个单词
【发布时间】:2012-11-19 06:12:36
【问题描述】:

我必须突出显示 JEditorPane 中所有出现的单词。为此,我使用以下代码:

 try
{          
javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
    new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
textPane.getHighlighter().addHighlight(startPos, endPos, 
highlightPainter);
}
catch(Exception ex)
{
}

但是我怎样才能给出一个单词的索引位置呢?

我正在从文件中读取内容,但它也在读取 HTML 标记,这会干扰单词的索引。

【问题讨论】:

    标签: java swing jeditorpane


    【解决方案1】:

    基本上,您应该能够在文档中查找您需要的匹配项...

    public class TestEditorPane01 {
    
        public static void main(String[] args) {
            new TestEditorPane01();
        }
    
        public TestEditorPane01() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JEditorPane editor = new JEditorPane();
                    try {
                        editor.setPage(new File("Test.html").toURI().toURL());
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(editor));
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    Document document = editor.getDocument();
                    try {
                        String find = "Method";
                        for (int index = 0; index + find.length() < document.getLength(); index++) {
                            String match = document.getText(index, find.length());
                            if (find.equals(match)) {
                                javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter =
                                        new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
                                editor.getHighlighter().addHighlight(index, index + find.length(),
                                        highlightPainter);
                            }
                        }
                    } catch (BadLocationException ex) {
                        ex.printStackTrace();
                    }
    
                }
            });
        }
    }
    

    这将遍历整个文档并突出显示所有匹配项。这也是区分大小写的匹配;)

    【讨论】:

    • 在哪个包文档类中可用。
    • 好的,但是当我调用 document.getText(0,document.getLength());它没有返回任何东西。
    • 应该是document.getText(0,document.getLength() - 1),否则可能会抛出异常。如果失败,我需要查看一些代码...
    【解决方案2】:

    这是我的情况,需要在 EditorPane 中突出显示一个搜索词:

        // text in EditPane
        String text = rSyntaxTextArea.getText();
        if (text != null && !"".equals(filterText.getText())) {
            Highlighter hilit = new RSyntaxTextAreaHighlighter();
            rSyntaxTextArea.setHighlighter(hilit);  
            for (int index = text.toUpperCase().indexOf(
                    // searched text
                    filterText.getText().toUpperCase()); index >= 0; index = text
                    .toUpperCase().indexOf(
                            filterText.getText().toUpperCase(), index + 1)) {
                int end = index + filterText.getText().length();
                HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(
                        Color.LIGHT_GRAY);
                try {
                    hilit.addHighlight(index, end, painter);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      您可以执行以下操作:

      getHighlighter().addHighlight(start, end, 
               new DefaultHighlighter.DefaultHighlightPainter(Color.red));
      

      【讨论】:

        猜你喜欢
        • 2012-10-31
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-12
        • 1970-01-01
        相关资源
        最近更新 更多