The Java Developers Almanac 1.4


Order this book from Amazon.

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

e1014. Determining If a Style Attribute Applies to a Character or the Paragraph

All style attributes as defined in StyleConstants are either character- or paragraph-based attributes. See also e1010 Listing the Attributes in a Style.
    boolean b;
    
    // Check if character-based attribute
    b = StyleConstants.Italic instanceof AttributeSet.CharacterAttribute;      // true
    b = StyleConstants.LineSpacing instanceof AttributeSet.CharacterAttribute; // false
    
    // Check if paragraph-based attribute
    b = StyleConstants.LineSpacing instanceof AttributeSet.ParagraphAttribute; // true
    b = StyleConstants.Italic instanceof AttributeSet.ParagraphAttribute;      // false
It is also possible to determine if the attribute is a color or a font-related attribute.
    // Check if color-based attribute
    b = StyleConstants.Foreground instanceof AttributeSet.ColorAttribute; // true
    b = StyleConstants.Italic instanceof AttributeSet.ColorAttribute;     // false
    
    // Check if font-based attribute
    b = StyleConstants.Italic instanceof AttributeSet.FontAttribute;      // true
    b = StyleConstants.Foreground instanceof AttributeSet.FontAttribute;  // false

 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
e1013. Automatically Updating Styled Text When a Style Is Updated

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


© 2002 Addison-Wesley.