[英]Highlight JTextArea
i have a problem with: 我有一个问题:
int index = text.indexOf(myWord);
while (index >= 0) {
int len = pattern.length();
hl.addHighlight(index, index + len, DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + len);
}
when I reach the end of line highlighting is still enable, how to disable highlight ? 当我到达行尾突出显示仍然启用时,如何禁用突出显示? The result of above code: 上面代码的结果:
Thanks for answer but the problem is: 谢谢你的回答,但问题是:
The black line is a place where I clicked and start typing, it is just bug. 黑线是我点击并开始输入的地方,这只是错误。
Well I had to do some assumptions on your variable names and I hope I did it right. 好吧,我不得不对你的变量名做一些假设,我希望我做得对。 Basically your code worked for me but I didn't understand the first line: 基本上你的代码对我有用,但我不理解第一行:
int index = text.indexOf(myWord);
I think it should be: 我认为它应该是:
int index = text.indexOf(pattern);
Anyway, I wrote this code and it worked for me: 无论如何,我写了这段代码,它对我有用:
String pattern = "<aa>";
String text = textArea.getText();
int index = text.indexOf(pattern);
while(index >= 0){
try {
hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + pattern.length());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
Update 更新
Look when you click on the end of marked text and start typing the text will be also marked- it is a my bug. 当你点击标记文本的结尾并开始输入时,看起来也会被标记 - 这是我的错误。
Sorry I didn't see that before. 对不起我之前没有看到。 I don't think you can stop a highlighter but you can make it using a CaretListener
and doing the stuff there. 我不认为你可以停止荧光笔,但你可以使用CaretListener
并在那里做一些东西。 This way if you input a new string that matches your pattern it will be highlighted too: 这样,如果您输入与您的模式匹配的新字符串,它也会突出显示:
textArea.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
if(e.getMark() == e.getDot()){
Highlighter hl = textArea.getHighlighter();
hl.removeAllHighlights();
String pattern = "<aa>";
String text = textArea.getText();
int index = text.indexOf(pattern);
while(index >= 0){
try {
Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + pattern.length());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
}
});
Note e.getMark() == e.getDot()
means "there's no text selection" based on CaretEvent.getDot() and CaretEvent.getMark() javadoc. 注意e.getMark() == e.getDot()
表示基于CaretEvent.getDot()和CaretEvent.getMark() javadoc的“没有文本选择”。 Otherwise you shouldn't highlight anything but selected text. 否则,除了选定的文本外,您不应突出显示任
Screenshot 截图
Updated SSCCE 更新了SSCCE
Here's a complete SSCCE to test it: 这是一个完整的SSCCE来测试它:
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
public class Demo {
private void initGUI(){
final JTextArea textArea = new JTextArea();
textArea.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
if(e.getMark() == e.getDot()){
Highlighter hl = textArea.getHighlighter();
hl.removeAllHighlights();
String pattern = "<aa>";
String text = textArea.getText();
int index = text.indexOf(pattern);
while(index >= 0){
try {
Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + pattern.length());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
}
});
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(300,200));
JPanel content = new JPanel(new FlowLayout());
content.add(scrollPane);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().initGUI();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.