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
  • 41,067
  • 12
  • 101
  • 141
Borut Flis
  • 14,525
  • 27
  • 81
  • 110

1 Answers1

4

I think that tutorial about JTextComponents contains detailed descriptions about that

Community
  • 1
  • 1
mKorbel
  • 109,107
  • 18
  • 130
  • 305
  • 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