问题
I'm trying to highlight multiple words in jTextPane but with no luck. So far I made this:
Highlighter h = jTextPane1.getHighlighter();
h.removeAllHighlights();
String text = jTextPane1.getText();
String words[] = text.split(" ");
for(int i = 0;i<words.length;i++){
String temp = words[i];
if(temp.equals("word")){
try{
h.addHighlight(i, temp.length(), DefaultHighlighter.DefaultPainter);
}
catch(Exception e){
}
}
}
But this only highlights the first word. How to select all found words?
回答1:
Instead of i
use text.indexOf(temp)
there
h.addHighlight(i, temp.length(), DefaultHighlighter.DefaultPainter);
来源:https://stackoverflow.com/questions/14086165/jtextpane-highlight-multiple-words