Highlight a word in JEditorPane
Asked Answered
H

3

1

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.

Hoofbound answered 19/11, 2012 at 6:12 Comment(0)
B
10

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

Broaddus answered 19/11, 2012 at 6:56 Comment(3)
in which package document class is available .Hoofbound
ok but when i calling document.getText(0,document.getLength()); its not returning anything.Hoofbound
It should be document.getText(0,document.getLength() - 1) otherwise it may throw an exception. If that fails, I'd need to see some code...Broaddus
M
3

Here's my case, need to highlight one search word in 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();
            }
        }
    }

Hope that helps.

Mohr answered 19/11, 2012 at 6:24 Comment(0)
F
1

You can do something as below:

getHighlighter().addHighlight(start, end, 
         new DefaultHighlighter.DefaultHighlightPainter(Color.red));
Fallacious answered 19/11, 2012 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.