The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > Styles  [9 examples]

e1013. 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);
                }
            }
        }
    }

 Related Examples
e1006. Highlighting Words in a JTextComponent
e1007. Setting the Font and Color of Text in a JTextPane Using Styles
e1008. Sharing Styles Between JTextPanes
e1009. Listing the Styles Associated with a JTextPane
e1010. Listing the Attributes in a Style
e1011. Using a Popup to Apply Styles to a JTextPane
e1012. Retaining the Logical Style When Setting a New Paragraph Style
e1014. Determining If a Style Attribute Applies to a Character or the Paragraph

See also: Actions and Key Bindings    Caret and Selection    Events    JEditorPane    JFormattedTextField    JTextArea    JTextComponent    JTextField    JTextPane   


© 2002 Addison-Wesley.