JTextPane highlight multiple words

不问归期 提交于 2019-12-24 20:05:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!