Alen
Alen

Reputation: 1788

JTextPane highlight multiple words

I'm trying to highlight multiple words in jTextPane but with no luck. So far I made this:

Highlighter h = jTextPane1.getHighlighter();
        h.removeAllHighlights();
        String text = jTextPane1.getText();
        String words[] = text.split(" ");
    for(int i = 0;i<words.length;i++){
        String temp = words[i];
        if(temp.equals("word")){
            try{
            h.addHighlight(i, temp.length(), DefaultHighlighter.DefaultPainter);
            }
            catch(Exception e){
            }
        }
    }

But this only highlights the first word. How to select all found words?

Upvotes: 0

Views: 1665

Answers (1)

StanislavL
StanislavL

Reputation: 57421

Instead of i use text.indexOf(temp) there

h.addHighlight(i, temp.length(), DefaultHighlighter.DefaultPainter);

Upvotes: 2

Related Questions