简体   繁体   English

在Java记事本中找到单词jtextfield

[英]Find a word jtextfield in java notepad

What condition should i put so that it highlights all the words in the JTextArea? 我应该设置什么条件以使其突出显示JTextArea中的所有单词? This code works without the while loop but it only finds and highlights the first word match. 这段代码没有while循环,但是只查找并突出显示第一个单词匹配。

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM