AttributeSets and Styles
AttributeSet and its relatives are used to hold collections of
attributes that can be used by styled text components (including
JTextPane). For example, an AttributeSet might comprise an attribute for
font size, an attribute for foreground color, and an attribute for
indentation. Each attribute is simply a key/value pair. The Document model keeps track of which attribute
sets apply to which blocks of text.
The interfaces and classes that are used for attribute sets are
shown in Figure 22-2. We’ll
discuss each one in detail, but first we’ll provide a brief overview of
what they do and how they relate. At the end of this section, we’ll
develop a Style-based text editor
example.
AttributeSetThis interface defines basic methods for accessing a read-only set of attributes. An
AttributeSetmay have a “resolving parent,” which (if it exists) is consulted when property lookups can’t be resolved by the current set.MutableAttributeSetThis interface extends
AttributeSetwith methods that allow attributes to be added, given new values, or deleted from the set.StyleThis interface extends
MutableAttributeSetto add two things: an optional name for the style and support for adding and removingChangeEventListeners.SimpleAttributeSetA basic implementation of the
MutableAttributeSetinterface.StyleConstantsThis class defines the standard attribute keys used by Swing’s text components. It also defines some static utility methods for getting and setting attribute values from ...