Hey, everyone, as you can see, first post. I'm just stumped. I've tried everything I can think of to highlight certain keywords as the user types in a non-CPU intensive manner in JTextPane with a StyledDocument. I'm almost sure there is a logical way that I cannot see, but here is what I have so far. The Patterns are already made and keywords are obviously found in there. When I run it, it highlights the first line perfectly. Second line, screws up by on (I'm guessing something with \n, but I really do not know). By the time I'm in the middle, if its already not on the keywords style (you can see what I mean in the code) as I type, the highlighting is way off. Thanks. Just a note - I'm 14, so if the answer is obvious, don't call me an idiot, etc.
private final Thread highlightKeywords = new Thread() {
private Style keywords;
@Override
public void run() {
for (final Pattern p : patterns) {
final Matcher m = p.matcher(text.getText());
while (m.find()) {
if (keywords == null) {
keywords = text.getStyle("Keywords");
}
doc.setCharacterAttributes(m.start(),
(m.end() - 1) - m.start(), keywords, true);
}
}
}
};