java - 如何在JTextPane中实现查找文 native 制?

标签 java find highlight jtextpane

我想在我的 JTextPane 中实现查找机制(如文本编辑器或单词)。 我希望它有下一个/上一个选项(向上/向下箭头)并突出显示它找到的所有单词。 有没有简单的方法可以做到这一点?

最佳答案

我不是专家,我发现以下代码有效:-

    public static void GetTextToFindAndFind(String textToFind, int ignorecase, int findCounter){
    // findCounter = 0 or 1. 0 represents find and 1 represents findCounter.
    String Current2 = textPane.getText();
    if(findCounter ==0){

        if(textToFind == null){
            optionPane.showMessageDialog(null, "Please Enter Text.", "Error", 0);
        }
        else if(textToFind.isEmpty()){
            optionPane.showMessageDialog(null, "Please Enter Text.", "Error", 0);
        }
        else{
            // Use any Character. But I a suggest to use a character from an Encrypted file.   
            Replacer = "¥";
            CurrentText = textPane.getText();
            if(ignorecase==1){
                CurrentText = CurrentText.toLowerCase();
                textToFind = TextToFind.toLowerCase();
            }
            int counter = 0;
            readtext = new StringReader(CurrentText);
            readBuffer = new BufferedReader(readtext);
            try {
                String Line = readBuffer.readLine();
                int found = 0;
                while(Line!=null || found != 1){
                    if(Line.contains(TextToFind)){
                        Line = null;
                        found = 1;
                    }
                    if(Line!=null){
                        Line = readBuffer.readLine();
                        counter = counter + 1;
                    }
                }
                if(found == 1){
                    textPane.setSelectionStart(CurrentText.indexOf(textToFind) - counter);
                    textPane.setSelectionEnd(CurrentText.indexOf(textToFind) + textToFind.length() - counter);
                    int counter2 = 1;
                    while(counter2!=textToFind.length()){
                        Replacer = Replacer + "¥";
                        counter2 = counter2 + 1;
                    }
                    CurrentText = CurrentText.replaceFirst(textToFind, Replacer);
                    findCounter = 1;
                            }
                else{
                    optionPane.showMessageDialog(null, "No Matches.", "Message", 0);    
                    }

                }
                catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  catch(NullPointerException e){
                optionPane.showMessageDialog(null, "No Matches.", "Message", 0);
            }
            }
        }

    else{
        int counter = 0;
        readtext = new StringReader(CurrentText);
        readBuffer = new BufferedReader(readtext);
        try {
            String Line = readBuffer.readLine();
            int found = 0;
            while(Line!=null || found != 1){
                if(Line.contains(textToFind)){
                    Line = null;
                    found = 1;
                }
                if(Line!=null){
                    Line = readBuffer.readLine();
                    counter = counter + 1;
                }
            }
            if(found == 1){
                textPane.setSelectionStart(CurrentText.indexOf(textToFind) - counter);
                textPane.setSelectionEnd(CurrentText.indexOf(textToFind) + textToFind.length() - counter);
                CurrentText = CurrentText.replaceFirst(textToFind, Replacer);
                        }
            else{
                optionPane.showMessageDialog(null, "No Matches.", "Message", 0);    
                }
            }
             catch(IOException e){
                e.printStackTrace();
            } catch(NullPointerException e){
                optionPane.showMessageDialog(null, "No Matches.", "Message", 0);    
                }
            }
    }

关于java - 如何在JTextPane中实现查找文 native 制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9771292/

相关文章:

vba - .Findnext 不尊重范围集

java - 突出显示方括号内的文本(正则表达式?)Android kotlin

css - 在所有页面中使用部分 html-body

java - Couchbase 服务器允许并发文档突变,即使设置了 CAS?

linux - 查找命令,linux

linux - 查找文件并将它们 tar(带空格)

ios - 让 UITableViewCell 短暂闪烁

java条码阅读器

java - Spring-WS 内容大小限制与 dataHandler

java - 关于Java继承的问题