문제

What condition should i put so that it highlights all the words in the JTextArea? This code works without the while loop but it only finds and highlights the first word match.

String findstr = findTextField.getText().toUpperCase(); // User Input Word to find
int findstrLength = findstr.length();                   
String findtextarea = textarea.getText().toUpperCase(); // TextArea Content
Highlighter h = textarea.getHighlighter();
h.removeAllHighlights();
try
    {
        int index=0;
        while(index>=0)                             // What should I put here ??
        {
            index = findtextarea.indexOf(findstr,index);
            h.addHighlight(index,index+findstrLength, DefaultHighlighter.DefaultPainter);
        }
    }
도움이 되었습니까?

해결책

    while(index>=0) {
        index = findtextarea.indexOf(findstr,index);
        if (index > 0) {
           h.addHighlight(index,index+findstrLength, DefaultHighlighter.DefaultPainter);
        }
        index++; // try adding this to allow you to look for the next index.
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top