2017-12-05 2 views
0

JTextPaneを使って簡単なハイライトを実装しています。私はそれを入力するとif,caseなどのような特別なキーワードを強調表示したい。 IDEのように。新しく追加されたテキストにSwing Highlighterがこぼれないようにする

私が直面している問題は、特別なキーワードが書かれただけで書かれていて、それが強調表示されている(これはうまくいく)ということです。 APIを見ると、これは当てはまりません。これを避ける方法は?

if (word.equals("if")) {       
    textPane.getHighlighter().addHighlight(wordStartIdx, pos+1, highlightPainter); 
    textPane.getHighlighter().addHighlight(pos+2, pos+2, noHightlighter); // tried this to see if it would help, but it doesn't change anything.. anything typed after the word "if" will still be hightlighted! 
} 

おかげ代わりに属性を使用して試すことができ蛍光ペンを使用しての

+0

後、適切な[MCVE]問題を示し

は、ここで問題のあるコードです。 – camickr

答えて

2

SimpleAttributeSet green = new SimpleAttributeSet(); 
StyleConstants.setForeground(green, Color.GREEN); 

// Change attributes on some existing text 

StyledDocument doc = textPane.getStyledDocument(); 
int offset = textPane.getCaretPosition(); // should be after the "f" 
doc.setCharacterAttributes(offset-2, 2, green, false); 
+0

私はアトリビュートセットについて読んだところ、ハイライトライターとアトリビュートセットの賛否両論は何ですか?彼らは彼らが提供するもので似ているようです。 –

関連する問題