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.

AttributeSet

This interface defines basic methods for accessing a read-only set of attributes. An AttributeSet may have a “resolving parent,” which (if it exists) is consulted when property lookups can’t be resolved by the current set.

MutableAttributeSet

This interface extends AttributeSet with methods that allow attributes to be added, given new values, or deleted from the set.

Style

This interface extends MutableAttributeSet to add two things: an optional name for the style and support for adding and removing ChangeEventListeners.

SimpleAttributeSet

A basic implementation of the MutableAttributeSet interface.

StyleConstants

This 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 ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.