The Java Developers Almanac 1.4


Order this book from Amazon.

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

e1010. Listing the Attributes in a Style

The way to retrieve the attributes in a style is to enumerate all the attribute names in the style and then use the specific attribute name to retrieve its value. The attribute name can be either a string or an object of type StyleConstants.
    // To retrieve a style from a text pane, see
    // e1009 Listing the Styles Associated with a JTextPane
    
    // Get number of attributes
    int count = style.getAttributeCount();
    
    // Get enumeration of attribute names; an attribute name can be
    // either a string or a StyleConstants object
    Enumeration enum = style.getAttributeNames();
    while (enum.hasMoreElements()) {
        Object o = enum.nextElement();
        if (o instanceof String) {
            String attrName = (String)o;
            Object attrValue = style.getAttribute(attrName);
        } else if (o == StyleConstants.NameAttribute) {
            // Retrieve the style's name
            String styleName = (String)style.getAttribute(o);
        } else if (o == StyleConstants.ResolveAttribute) {
            // Retrieve the style's parent
            Style parent = (Style)style.getAttribute(o);
        } else {
            // Retrieve the style constant name and value
            String attrName = o.toString();
            Object attrValue = style.getAttribute(o);
        }
    }

 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
e1011. Using a Popup to Apply Styles to a JTextPane
e1012. Retaining the Logical Style When Setting a New Paragraph Style
e1013. Automatically Updating Styled Text When a Style Is Updated
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.