![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e1013. Automatically Updating Styled Text When a Style Is UpdatedWhen 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 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 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); } } } }
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
© 2002 Addison-Wesley. |