TravJav92
TravJav92

Reputation: 117

Highlight Text on JTextArea

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.

I seem to be having a problem with the loop itself it highlights the first occurrence and then none after that.

    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 >=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 >=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
}

 }   

Upvotes: 0

Views: 1504

Answers (1)

gpasch
gpasch

Reputation: 2682

Yeah do this search for the right string:

while(index >=0){
  System.out.println("looping");
  int len = jtf.length();
  hilit.addHighlight(index,index+len, painter);
  index = jta.indexOf(jtf, index+len);
}

--

I made a little program based on your code: the only problem is as I said that you were looking for the wrong string (you didnt change jta to jtf in indexOf(). Otherwise it works ok and highlights spotlight with red (my color you can set it to what you want):

class project extends Frame { // implements ActionListener{

 Highlighter hilit;
 Highlighter.HighlightPainter painter;

JTextArea userField;
JTextField searchT;
JButton searchB;

public project(){


 userField = new JTextArea("spotlight aaa spotlight");
 searchT = new JTextField("spotlight");
 searchT.setEditable(true);
//     searchT.setBorder(new LineBorder (Color.black));

 searchB = new JButton("Search");

    hilit = new DefaultHighlighter();
    painter = new                    DefaultHighlighter.DefaultHighlightPainter(Color.red);





// 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 >=0){
System.out.println("looping");


System.out.println("im searching for"+jtf+" "+index);
int len = jtf.length();
hilit.addHighlight(index,index+len, painter);
index = jta.indexOf(jtf, index+len);
System.out.println("this is what I want to highlight"+ index);
// remove hightlights with a timer for example after 10 seconds
}

}

});

setSize(500, 500);

setLayout(new FlowLayout());

add(userField);
add(searchT);
add(searchB);

setVisible(true);

}

}

Upvotes: 1

Related Questions