title: Multiple Word Highlighting in Java
body: <p>I'm trying to make a word search engine that finds words with a wildcard search, and highlights all of the words it finds. I have got the search going, but the highlighter will only highlight the first word it finds. Say I search for '*hat', all of the instances of 'that' are highlighted, but it does not highlight any other words such as 'what' (even though they are returned to the console as found).</p>
  
  <p>Here is my code for the highlighter, the string returned by the pattern matcher are passed to this method:</p>
  
  <pre><code>public static void highlighter(String s){
      String text = displayString.getText();
      highlightStr = new DefaultHighlighter();
      painter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
      displayString.setHighlighter(highlightStr); 
      //highlightStr.removeAllHighlights();
  
      for(int index = 0; index &lt; text.length(); index++){        
          int start = text.indexOf(s, index);
  
          try{
              int end = start + s.length();
              highlightStr.addHighlight(start, end, painter);
              displayString.setCaretPosition(end);
          }catch(BadLocationException e){} 
      }
  }
  </code></pre>
  
  <p>To be sure that the strings are all being passed to the highlighter I have printed them from the method.</p>