Reputation: 11
i want to color some keywords in JTextPane. But I dont want to keep the same style after these words
while (regexMatcher.find()){
int start = regexMatcher.start();
int end = regexMatcher.end();
document.setCharacterAttributes(start, end-start, style, false);
}
It works however when I click right after the last char and type something that has same style but want to change back to default.
How should I resolve this?
Upvotes: 0
Views: 73
Reputation: 57381
You can add a CaretChangeListener
to the JTextPane
. On each caret update you should clear input attributes of the EditorKit installed in the JTextPane
.
MutableAttributeSet inputAttributes=((StyledEditorKit)pane.getEditorKit()).getInputAttributes();
//remove all the unwanted style attributes
Upvotes: 1