问题
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.
回答1:
I think that tutorial about JTextComponents contains detailed descriptions about that
you have extract Document from JTextComponents,
then you can styled text into JTextPane, basically there are two ways use Highlighter and StyledDocument
for Html formatted text, you have to create own HtlmEditorKit
来源:https://stackoverflow.com/questions/9878085/select-multiple-items-in-jtextpane