查找/替换突出显示的单词Netbeans/Java [英] Find/replace Highlight replaced words Netbeans/Java
问题描述
任何想法如何突出显示JTextpane中被替换的单词.我尝试了许多选项,但得到的最接近的结果是,仅单词的首次出现被突出显示,其他突出显示被移动.我用下面的方法.我已经使用它来查找和突出显示单词.因此,我认为如果可以对其进行一些修改,那么我也可以将其用于查找和替换方法.谢谢.
Any ideas how to highlight the replaced words in JTextpane. I tried many options but the closest I get is that only the first occurrence of the word is highlighted, the other highlights are shifted. I used method below. I already used it for finding and highlighting words. So I thought if I could modify it a bit, I could also use it also for find and replace method. Thanks.
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){
}
推荐答案
我得到的最接近的是仅单词的第一次出现被突出显示,其他突出显示被移位
the closest I get is that only the first occurrence of the word is highlighted, the other highlights are shifted
int i = textpane.getText().indexOf(find, 0);
请勿使用textPane()getText()
.文本中将包含"\ r \ n".但是,文档仅具有"\ n",因此用于突出显示的索引每增加一行将被关闭一个.
Don't use textPane()getText()
. This will include "\r\n" in the text. However, the Document only has "\n" so then index used for the highlighting will be off by one for each additional line.
您已经使用以下方法从Dcoument中获取了文字:
You already have the text from the Dcoument using:
String text = doc.getText(0, doc.getLength());
所以只需在文本"变量上进行搜索.
so just do the searching on the "text" variable.
查看文本和新行更详细的信息.
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();
}
});
}
}
请注意,这不是一个非常有效的解决方案,因为每次更改时都需要从文档中获取文本,以确保搜索文本字符串时的偏移量与文档中的文本保持同步.
Note that this is not a very efficient solution because you need to get the text from the Document every time a change is made to make sure the offsets when searching the text string are in sync with the text in the Document.
Edit2:
使用文档中的初始文本字符串的更高效的查找/替换算法:
A more efficient find/replace algorithm that uses the initial text string from the Document:
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) {}
}
});
这篇关于查找/替换突出显示的单词Netbeans/Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!