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?

[1589 byte] By [AlesCernanskya] at [2007-10-3 9:38:37]
# 1
Use a highlighter to highlight the selected text: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=707928
camickra at 2007-7-15 4:54:26 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks,it seems like good idea. I will try it. But I am still interested why does not work my solution described above.
AlesCernanskya at 2007-7-15 4:54:26 > top of Java-index,Desktop,Core GUI APIs...