Get attribute set of given character in JTextPane
Hi,
i am developing simple text editor. One of its functionalities is to highlight found text pattern. Before text highlighting i am trying to save original text style to restore it when highlighting is canceled. But it does not work :-(
1. For each of highlighted characters save the original attribute set:
AttributeSet attributeSet =
getStyledDocument().getCharacterElement(aStartIndex + i).getAttributes();
originalAttributeList.add(attributeSet);
2. Highlight the text:
// For each character set the background to yellow
AttributeSet attributeSet
getStyledDocument().getCharacterElement(aStartIndex + i).getAttributes();
SimpleAttributeSet style =new SimpleAttributeSet(attributeSet);
StyleConstants.setBackground(style, Color.YELLOW);
styledDocument.setCharacterAttributes(aStartIndex + i, 1, style,true);
3. Restore original attributes when highlighting is canceled:
// For each character
AttributeSet attributeSet = originalAttributeList.get(i);
getStyledDocument().setCharacterAttributes(
aStartIndex + i,
1,
attributeSet,
true);
The original font was bold but when is restored some of the characters are plain and different color. It seems like these characters take over the style of preceding text. I am 99% sure there is no mistake in text indexs.
Can anybody help?

