title: Highlight Text on JTextArea
body: <p>I need to highlight text on a JTextArea Im using JTextField to get users input and then a Button to grab the JTextFields text. Ultimately I need to make it so it highlights all occurrences of the String in JTextArea exp: I have 200 words and 10 occurrences of the word 'dog' I want dog to be highlighted 10 times.</p>
  
  <p>I seem to be having a problem with the loop itself it highlights the first occurrence and then none after that.</p>
  
  <pre><code>    private void getSearch() throws BadLocationException {
  
                   // my textfield (searchT)
                 String jtf = searchT.getText();
  
                  // my jtextarea  (userField);
                  String jta = userField.getText();
  
                  hilit = userField.getHighlighter();
                  hilit.removeAllHighlights();
  
  
           int index = jta.indexOf(jtf);
  
          while(index &gt;=0){
          System.out.println("looping");
  
         int len = jtf.length();
          hilit.addHighlight(index,index+len, painter);
          index = jta.indexOf(jta, index+len);
  
  
      }
  
       }   
  
  
  
  
  **PER REQUEST:**
  
  
  public class project extend Frame implements ActionListener{
  
     Highlighter hilit;
     Highlighter.HighlightPainter painter;
  
  
  
  public project(){
  
  
       userField = new JTextArea();
       searchT = new JTextField("Spotlight");
       searchT.setEditable(true);
       searchT.setBorder(new LineBorder (Color.black));
  
       searchB = new JButton("Search");
  
          hilit = new DefaultHighlighter();
          painter = new                    DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
  
  
  
  
  
  // JButton (searchB) with ActionListener 
   searchB.addActionListener(new java.awt.event.ActionListener(){
      public void actionPerformed(java.awt.event.ActionEvent evt) {
          try {
              searchBActionPerformed(evt);
          } catch (BadLocationException ex) {
              Logger.getLogger(DaWord.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
             private void searchBActionPerformed(ActionEvent evt) throws BadLocationException  {
                 Object sb = evt.getSource();
               if(searchB==sb){
             getSearch();
  
                 }
  
             }
             private void getSearch() throws BadLocationException {
  
               // my jtextfield
             String jtf = searchT.getText();
              // my jtextarea
              String jta = userField.getText();
  
              hilit = userField.getHighlighter();
              hilit.removeAllHighlights();
  
  
       int index = jta.indexOf(jtf);
  
      while(index &gt;=0){
      System.out.println("looping");
  
  
      System.out.println("im searching for"+index);
      int len = jtf.length();
      hilit.addHighlight(index,index+len, painter);
      index = jta.indexOf(jta, index+len);
      System.out.println("this is what I want to highlight"+ index);
      // remove hightlights with a timer for example after 10 seconds
  }
  
   }   
  </code></pre>