O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required