user3544619
user3544619

Reputation: 11

JTextPane - turning off last Style

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

Answers (1)

StanislavL
StanislavL

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

Related Questions