Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

java - Highlight a word in 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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...