查找/替换突出显示替换的单词Netbeans/Java

查找/替换突出显示替换的单词Netbeans/Java,java,swing,netbeans,Java,Swing,Netbeans,如何在JTextpane中突出显示替换的单词。我尝试了许多选项,但最接近的是,只有单词的第一个出现被突出显示,其他突出显示被移动。我使用了下面的方法。我已经用它来查找和突出单词了。所以我想,如果我能对它稍加修改,我也可以将它用于查找和替换方法。谢谢 Highlighter.HighlightPainter PaintChange = new PaintFind(Color.yellow); try { Highlighter color = textpane.g

如何在JTextpane中突出显示替换的单词。我尝试了许多选项,但最接近的是,只有单词的第一个出现被突出显示,其他突出显示被移动。我使用了下面的方法。我已经用它来查找和突出单词了。所以我想,如果我能对它稍加修改,我也可以将它用于查找和替换方法。谢谢

Highlighter.HighlightPainter PaintChange = new PaintFind(Color.yellow);

    try {
            Highlighter color = textpane.getHighlighter();
            String find = FieldFind.getText();
            String replace = FieldReplace.getText();
            Document doc = textpane.getDocument();
            String text = doc.getText(0, doc.getLength());
            int pos = 0;
            int counter = 0;

            while((pos=text.toUpperCase().indexOf(find.toUpperCase(), pos))>=0) {

                int i = textpane.getText().indexOf(find, 0);
                textpane.select(i, i+find.length());
                text.replaceSelection(FieldReplace.getText());

                color.addHighlight(pos, pos+replace.length(), PaintFind);
                pos += find.length();

                counter++;
            }
            status.setText("Nuber of changed words: " + " " + Integer.toString(counter));

        } catch(Exception e){

        }
我得到的最接近的结果是,只有单词的第一次出现被突出显示,其他突出显示被移动

不要使用
textPane()getText()
。这将在文本中包含“\r\n”。但是,文档只有“\n”,因此用于突出显示的索引将每增加一行关闭一个

您已经使用以下命令从数据文档中获取了文本:

String text = doc.getText(0, doc.getLength());
所以只需搜索“text”变量

查看更多详细信息

编辑:

请注意,这不是一个非常有效的解决方案,因为每次进行更改时都需要从文档中获取文本,以确保搜索文本字符串时的偏移量与文档中的文本同步

编辑2:

使用文档中的初始文本字符串的更高效的查找/替换算法:

    findReplace.addActionListener( new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                textPane.getHighlighter().removeAllHighlights();
                String findText = find.getText();
                int findLength = findText.length();
                String replaceText = replace.getText();
                int replaceLength = replaceText.length();

                Document doc = textPane.getDocument();
                String text = doc.getText(0, doc.getLength());
                int count = 0;
                int offset = 0;

                while ((offset = text.indexOf(findText, offset)) != -1)
                {
                    int replaceOffset = offset + ((replaceLength - findLength) * count);
                    textPane.select(replaceOffset, replaceOffset + findLength);
                    textPane.replaceSelection( replaceText );

                    textPane.getHighlighter().addHighlight(replaceOffset, replaceOffset + replaceLength, painter);
                    offset += replaceLength;
                    //text = doc.getText(0, doc.getLength());
                    count++;
                }
            }
            catch(BadLocationException ble) {}
        }
    });

如果我放置“text”变量而不是textpane.getText(),则只替换并突出显示第一个单词。我认为最主要的是pos变量,它在while循环中增加。我尝试了许多选择,但没有成功。并且不太理解“搜索文本变量”。你能解释一下吗?谢谢,我的意思是你不能使用textPane.getText()进行indexOf(…)搜索,因为你的偏移量会错误,原因我在
文本和新行链接中解释过。因此,您需要搜索文本变量,该变量需要在每次执行替换时在循环中刷新。请参见编辑。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class FindSSCCE extends JPanel
{
    JTextField find = new JTextField(10);
    JTextField replace = new JTextField(10);
    JTextPane textPane = new JTextPane();
    Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.cyan );

    public FindSSCCE()
    {
        setLayout( new BorderLayout() );

        JPanel north = new JPanel( new GridLayout(0, 2) );
        north.add( new JLabel("Find") );
        north.add( find );
        north.add( new JLabel("Replace") );
        north.add( replace );
        add(north, BorderLayout.NORTH);

        add(new JScrollPane(textPane));

        JButton findReplace = new JButton("Find and Replace");
        add(findReplace, BorderLayout.SOUTH);
        findReplace.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    String findText = find.getText();
                    int findLength = findText.length();
                    String replaceText = replace.getText();
                    int replaceLength = replaceText.length();

                    Document doc = textPane.getDocument();
                    String text = doc.getText(0, doc.getLength());
                    int offset = 0;

                    while ((offset = text.indexOf(findText, offset)) != -1)
                    {
                        textPane.select(offset, offset + findLength);
                        textPane.replaceSelection( replaceText );

                        textPane.getHighlighter().addHighlight(offset, offset + replaceLength, painter);
                        offset += replaceLength;
                        text = doc.getText(0, doc.getLength());
                    }
                }
                catch(BadLocationException ble) {}
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("FindSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new FindSSCCE() );
        frame.setLocationByPlatform( true );
        frame.setSize(400, 300);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
    findReplace.addActionListener( new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                textPane.getHighlighter().removeAllHighlights();
                String findText = find.getText();
                int findLength = findText.length();
                String replaceText = replace.getText();
                int replaceLength = replaceText.length();

                Document doc = textPane.getDocument();
                String text = doc.getText(0, doc.getLength());
                int count = 0;
                int offset = 0;

                while ((offset = text.indexOf(findText, offset)) != -1)
                {
                    int replaceOffset = offset + ((replaceLength - findLength) * count);
                    textPane.select(replaceOffset, replaceOffset + findLength);
                    textPane.replaceSelection( replaceText );

                    textPane.getHighlighter().addHighlight(replaceOffset, replaceOffset + replaceLength, painter);
                    offset += replaceLength;
                    //text = doc.getText(0, doc.getLength());
                    count++;
                }
            }
            catch(BadLocationException ble) {}
        }
    });