0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alen
  • 1,750
  • 7
  • 31
  • 62
  • Take a look at [this example](http://stackoverflow.com/questions/13448558/highlight-a-word-in-jeditorpane/13449000#13449000) it uses a JEditorPane, but I'm pretty sure, so long as your using a StyledDocument, it should work – MadProgrammer Dec 30 '12 at 02:52

1 Answers1

2

Instead of i use text.indexOf(temp) there

h.addHighlight(i, temp.length(), DefaultHighlighter.DefaultPainter);
StanislavL
  • 56,971
  • 9
  • 68
  • 98