2014-11-05 26 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 
相關問題