Automatically Updating Styled Text When a Style Is Updated

When a style is applied to text in a text pane, the text is set with the attributes of the style. If the style is changed, the set of attributes associated with the range of text does not change.

This example demonstrates how to have styled text automatically updated when the style object used to style the text is changed. When a named style is applied to text, the name of the style is included in the set of attributes that are associated with the text. Hence, it is possible to find all the places where a named style has been applied in a text pane. The reapplyStyle() method takes a style object and searches the text pane for all uses of the style (by name) and reapplies it.

In order to detect changes to a style, a change listener is added to it. Whenever the style is changed, the style's change listener will call reapplyStyle() to update the text pane.

final JTextPane textPane = new JTextPane(); ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent e) { reapplyStyles(textPane, (Style)e.getSource()); } }; // Make paragraph red Style style = textPane.addStyle("Highlight", null); style.addChangeListener(changeListener); StyleConstants.setForeground(style, Color.red); textPane.setParagraphAttributes(style, true); // paragraph appears red style = textPane.getStyle("Highlight"); StyleConstants.setUnderline(style, true); // paragraph becomes red and underlined // This method traverses every paragraph and content element // and reapplies any style that matches the specified style public static void reapplyStyles(JTextPane c, Style style) { // Get section element Element sectionElem = c.getDocument().getDefaultRootElement(); // Get number of paragraphs. int paraCount = sectionElem.getElementCount(); for (int i=0; i<paraCount; i++) { Element paraElem = sectionElem.getElement(i); AttributeSet attr = paraElem.getAttributes(); // Get the name of the style applied to this paragraph element; may be null String sn = (String)attr.getAttribute(StyleConstants.NameAttribute); // Check if style name match if (style.getName().equals(sn)) { // Reapply the paragraph style int rangeStart = paraElem.getStartOffset(); int rangeEnd = paraElem.getEndOffset(); c.getStyledDocument().setParagraphAttributes( rangeStart, rangeEnd-rangeStart, style, true); } // Enumerate the content elements for (int j=0; j<paraElem.getElementCount(); j++) { Element contentElem = paraElem.getElement(j); attr = contentElem.getAttributes(); // Get the name of the style applied to this content element; may be null sn = (String)attr.getAttribute(StyleConstants.NameAttribute); // Check if style name match if (style.getName().equals(sn)) { // Reapply the content style int rangeStart = contentElem.getStartOffset(); int rangeEnd = contentElem.getEndOffset(); c.getStyledDocument().setCharacterAttributes( rangeStart, rangeEnd-rangeStart, style, true); } } } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.