使用 Java 荧光笔突出显示多个匹配项
Using Java Highlighter to highlight multiple matches
我想让我的荧光笔突出显示与正则表达式的所有匹配项。我的正则表达式工作正常,并为荧光笔提供正确的开始和结束。但是,荧光笔会继续突出显示后续文本。见附图。
class CommandAreaDocumentListener implements DocumentListener {
Pattern variables
= java.util.regex.Pattern.compile("(?<variable>\$\w*\$)");
@Override
public void insertUpdate(DocumentEvent e) {
// System.out.println(commandArea.getText());
validate(commandArea.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
// System.out.println(commandArea.getText());
validate(commandArea.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
// System.out.println(commandArea.getText());
validate(commandArea.getText());
}
private void validate(String s) {
Matcher matcher = variables.matcher(s);
SwingUtilities.invokeLater(new HighlighterRunnable(matcher));
}
class HighlighterRunnable implements Runnable {
Matcher matcher;
public HighlighterRunnable(Matcher m) {
this.matcher = m;
}
@Override
public void run() {
// System.out.println("Running!");
while (matcher.find()) {
for (int i = 0; i < matcher.groupCount() + 1; i++) {
try {
// commandArea.append("\nGroup #" + i + ": " + matcher.group(i));
// commandArea.append(" , " + matcher.end(i));
int start = matcher.start(i);
int end = matcher.end(i);
commandArea.getHighlighter().addHighlight(start, end, DefaultHighlighter.DefaultPainter);
// if (matchCount == 1) inputArea.setCaretPosition(start);
} catch (BadLocationException ex) {
Logger.getLogger(CommandPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
}
不确定为什么我必须这样做,但是在 "run()" 中创建一个新的 DefaultHighlighter 解决了这个问题。不确定其他人如何使用我的代码看到不同的结果。这是修复:
@Override
public void run() {
// System.out.println("Running!");
commandArea.setHighlighter(new DefaultHighlighter());
while (matcher.find()) {
for (int i = 0; i < matcher.groupCount() + 1; i++) {
try {
int start = matcher.start(i);
int end = matcher.end(i);
System.out.println("start=" + start + " end=" + end);
commandArea.getHighlighter().addHighlight(start, end, new DefaultHighlighter.DefaultHighlightPainter(Color.CYAN));
} catch (BadLocationException ex) {
Logger.getLogger(CommandPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
我想让我的荧光笔突出显示与正则表达式的所有匹配项。我的正则表达式工作正常,并为荧光笔提供正确的开始和结束。但是,荧光笔会继续突出显示后续文本。见附图。
class CommandAreaDocumentListener implements DocumentListener {
Pattern variables
= java.util.regex.Pattern.compile("(?<variable>\$\w*\$)");
@Override
public void insertUpdate(DocumentEvent e) {
// System.out.println(commandArea.getText());
validate(commandArea.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
// System.out.println(commandArea.getText());
validate(commandArea.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
// System.out.println(commandArea.getText());
validate(commandArea.getText());
}
private void validate(String s) {
Matcher matcher = variables.matcher(s);
SwingUtilities.invokeLater(new HighlighterRunnable(matcher));
}
class HighlighterRunnable implements Runnable {
Matcher matcher;
public HighlighterRunnable(Matcher m) {
this.matcher = m;
}
@Override
public void run() {
// System.out.println("Running!");
while (matcher.find()) {
for (int i = 0; i < matcher.groupCount() + 1; i++) {
try {
// commandArea.append("\nGroup #" + i + ": " + matcher.group(i));
// commandArea.append(" , " + matcher.end(i));
int start = matcher.start(i);
int end = matcher.end(i);
commandArea.getHighlighter().addHighlight(start, end, DefaultHighlighter.DefaultPainter);
// if (matchCount == 1) inputArea.setCaretPosition(start);
} catch (BadLocationException ex) {
Logger.getLogger(CommandPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
}
不确定为什么我必须这样做,但是在 "run()" 中创建一个新的 DefaultHighlighter 解决了这个问题。不确定其他人如何使用我的代码看到不同的结果。这是修复:
@Override
public void run() {
// System.out.println("Running!");
commandArea.setHighlighter(new DefaultHighlighter());
while (matcher.find()) {
for (int i = 0; i < matcher.groupCount() + 1; i++) {
try {
int start = matcher.start(i);
int end = matcher.end(i);
System.out.println("start=" + start + " end=" + end);
commandArea.getHighlighter().addHighlight(start, end, new DefaultHighlighter.DefaultHighlightPainter(Color.CYAN));
} catch (BadLocationException ex) {
Logger.getLogger(CommandPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}