Java 如何以循环方式突出显示文本区域中的单词

Java 如何以循环方式突出显示文本区域中的单词,java,swing,search,jtextarea,Java,Swing,Search,Jtextarea,我编写了一个代码,用于在TextArea中查找一个特定单词并突出显示它。但是,它从插入符号位置到TextArea的结尾搜索并突出显示该单词。但是,我想从插入符号位置到TextArea的结尾再从TextArea的开头到结尾再从开始到结尾(循环方式)搜索该单词.我在这里贴了我的搜索方法。请检查一下。谢谢 我的搜索词方法: public void highLight(JTextArea component,String patteren){ try { Document doc

我编写了一个代码,用于在TextArea中查找一个特定单词并突出显示它。但是,它从插入符号位置到TextArea的结尾搜索并突出显示该单词。但是,我想从插入符号位置到TextArea的结尾再从TextArea的开头到结尾再从开始到结尾(循环方式)搜索该单词.我在这里贴了我的搜索方法。请检查一下。谢谢

我的搜索词方法:

public void highLight(JTextArea component,String patteren){
    try {
        Document doc=component.getDocument();
        String text=component.getText(0,doc.getLength());
        int pos=component.getCaretPosition();
        int index=text.toLowerCase().indexOf(patteren.toLowerCase(),pos);
        if (index>=0) {
            component.setSelectionStart(index);
            component.setSelectionEnd(index+patteren.length());
            component.getCaret().setSelectionVisible(true);
        }
    }
    catch(Exception e){
    }
}

如果没有发现任何问题,请使用文本的前一部分

以便更快地获得更好的帮助,发布一个(最简单的完整可验证示例)。它正在工作。非常感谢。
    int index=text.toLowerCase().indexOf(patteren.toLowerCase(),pos);
    if (index>=0) {
        component.setSelectionStart(index);
        component.setSelectionEnd(index+patteren.length());
        component.getCaret().setSelectionVisible(true);
    }
    else {
        index=text.substring(0,pos).toLowerCase().indexOf(patteren.toLowerCase());
        if (index>=0) {
            component.setSelectionStart(index);
            component.setSelectionEnd(index+patteren.length());
            component.getCaret().setSelectionVisible(true);
        }
    }