在JEditorPane中突出显示一个单词 [英] Highlight a word in JEditorPane

查看:78
本文介绍了在JEditorPane中突出显示一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to highlight the all occurrences of a word in JEditorPane . For this I am using the following code:

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

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

But how can I give the position of index of a word?

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

I am reading the content from file but it is reading the HTML tags also and it is disturbing the index of words.

推荐答案

基本上,您应该能够查找所需的匹配文件......

Basically, you should be able to walk the document looking for the match(es) you need...

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();
                }

            }
        });
    }
}

这将走完整个文档并突出显示所有匹配项。这也是一个案例敏感的比赛;)

This will walk the entire document and highlight all the matches. This is also a case sensitve match ;)

这篇关于在JEditorPane中突出显示一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆