2014-11-05 39 views
0

我想为JTextPane中的某些关键字着色。但我不想要保持相同的风格这话JTextPane - 关闭最后一个样式

while (regexMatcher.find()){ 
      int start = regexMatcher.start(); 
      int end = regexMatcher.end(); 

      document.setCharacterAttributes(start, end-start, style, false); 
     } 

它的工作原理后,但是当我的最后一个字符后,右键单击并键入一些有相同的风格,但想变回默认。

我应该如何解决这个问题?

回答

1

您可以将CaretChangeListener添加到JTextPane。在每个脱字符更新上,您应该清除安装在JTextPane中的EditorKit的输入属性。

MutableAttributeSet inputAttributes=((StyledEditorKit)pane.getEditorKit()).getInputAttributes(); 
//remove all the unwanted style attributes