2

I would like to highlight(select) multiple occurrences of a string in JTextPane. I would like to make something like findall. Here is what I have written.

 int a=0;
 while(jTextPane1.getText().indexOf(search,a)>0)
 {
     int i =jTextPane1.getText().indexOf(search,a);
     a=i+search.length();
     jTextPane1.select(i,a);
 }

It works ok, but the problem is that it highlights only the last occurrence, because the highlight changes. I would like to make multiple highlights.

DNA
  • 42,007
  • 12
  • 107
  • 146
Borut Flis
  • 15,715
  • 30
  • 92
  • 119

1 Answers1

4

I think that tutorial about JTextComponents contains detailed descriptions about that

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    The `TextFieldDemo` as described in the [Swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html) is the sample you are looking for, which basically does what @mKorbel described – Robin Mar 26 '12 at 20:49