Hello,
I try to do a program that can search whatever the user typed.
But I kind of having a problem, which I cannot select everything matches.

For example, I type "Hi there hello, what is your name ? please say hello"
The I click on the search button.
It ask what to find.
For example I want to search "hello"
but in here my program only highlight the first word hello, the hello at the end was not highlighted. I use TextArea.
please take a look at my code :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String search = javax.swing.JOptionPane.showInputDialog(this,"What do you want to search?");
        int start = jTextArea1.getText().indexOf(search);
        if (search != null)
        {
            jTextArea1.select(start, start + search.length());
            jTextArea1.requestFocus();
        }
    }

Thank you

What do you want the code to do differently?
What does the API doc for text area say about the select() method?
Are you assuming it is possible to select more than one area of text?

Does the select() method supports multiple selection?

Anyway, you are not searching through the whole string but stop at the first occurrence found.

int start = jTextArea1.getText().indexOf(search);
if (search != null) {
  jTextArea1.select(start, start + search.length());
  jTextArea1.requestFocus();
}

Should try this...

int start = jTextArea1.getText().indexOf(search);
while (start>-1) {  // if found substring, keep doing it
  jTextArea1.select(start, start + search.length());
  jTextArea1.requestFocus();
  start = jTextArea1.getText().indexOf(search, start+search.length());
}

@NormR1
Yes I expecting that it can select more than 1 words or more than 1 sentences.
For example, if I typed "Say hello please, and say it twice, please" into the TextArea
Then I search the word "please" then I want it to select and highlight those two please.

@Taywin
Hello,
I tried and it doesn't work. My program become not responding, I think it is infinite looping ?

I think it is infinite looping

Do some debugging to the program by adding println() statements to output how the variables are changing (or not changing) in the loop.

How to do debugging to the program?
I'm using netbeans and now, I'm totally confuse...

int start = jTextArea1.getText().indexOf(search);
System.out.println("search=" + search);  // Show what we're looking for
System.out.println("current text=" + jTextArea1.getText()); // show what's being searched
while (start>-1) {  // if found substring, keep doing it
  System.out.println("start=" + start);  // show where it was just found
  jTextArea1.select(start, start + search.length());
  jTextArea1.requestFocus();
  start = jTextArea1.getText().indexOf(search, start+search.length());
}

Oh just saw something too... Maybe this...

int start = jTextArea1.getText().indexOf(search);

should also be...

int start = search.indexOf(jTextArea1.getText());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.