2017-12-05 2 views
0

JTextPane 이상의 간단한 형광펜을 구현하고 있습니다. 내가 입력 할 때 if, case 등과 같은 특수 키워드를 강조하고 싶습니다. IDE가 그렇듯이.Swing Highlighters가 새롭게 추가 된 텍스트에 쏟아지는 것을 피하십시오

내가 직면하는 문제는 특수 키워드가 검색되었을 때만 표시되고 강조 표시하면 (잘 작동 함) 나중에 입력 한 모든 항목도 강조 표시됩니다. 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

속성 집합에 대해 읽었으며 궁금한 점이 무엇입니까? 형광펜 대 특성 집합을 사용하는 데있어서 장점/단점은 무엇입니까? 그들은 그들이 제공하는 것과 비슷한 것으로 보인다. –

관련 문제