Java Reference
In-Depth Information
Creating Styled Text
In Chapter 15, you looked at displaying plain text and HTML. With the Swing text components—
or at least the JTextPane —you can also display stylized text, in which different blocks of text
can have multiple attributes. These attributes might include boldface, italics, a different font
or color at the character level, or justification at the paragraph level, just as with any of the
modern word processors.
To support these capabilities, Swing supplies many different interfaces and classes, all of
which start with the specialized Document interface extension of StyledDocument . The Document
interface was introduced in Chapter 15, focusing on the PlainDocument implementation class.
The StyledDocument interface, or more precisely, the DefaultStyledDocument implementation,
manages a series of styles and attribute sets for the contents of a Document .
The various styles used by a StyledDocument are described initially by the AttributeSet
interface, which is a set of key/value pairs of read-only attributes. The key for an attribute
might be “current font,” in which the setting would be the font to use. To actually change the
font, you need to move on to the MutableAttributeSet interface, which supplies the ability to
add and remove attributes. For instance, if you had an AttributeSet for “bold,” you could use
MutableAttributeSet to also add italics, underlining, or colorization (or all three) to the set.
For a simple implementation of AttributeSet , there is the StyleContext.SmallAttributeSet
class, which uses an array to manage the set of attributes. For an implementation of the
MutableAttributeSet interface, there is the SimpleAttributeSet class, which uses a Hashtable
to manage the attributes. More complex attribute sets move on to the Style interface, which
adds a name to the set of attributes as defined by a MutableAttributeSet . The actual Style
implementation class is the StyleContext.NamedStyle class. Besides adding a name, the Style
interface adds the ability to have a ChangeListener monitor a set of attributes for changes.
The class that manages the set of Style objects for a StyledDocument is the StyleContext
class. An implementation of the AbstractDocument.AttributeContext interface, StyleContext
uses the StyleConstants class, which defines various attributes for commonly used styles.
When working with HTML documents, the StyleContext is actually a StyleSheet , which may
help you in understanding the whole arrangement. Keep in mind that all of the classes and
interfaces discussed here (except StyleSheet ) are required just to set up the Document data
model for a particular JTextPane .
StyledDocument Interface and DefaultStyledDocument Class
The StyledDocument interface extends the Document interface by adding the ability to store
styles for the content of the document. These styles can describe the character or paragraph
attributes, such as color, orientation, or font.
public interface StyledDocument extends Document {
public Style addStyle(String nm, Style parent);
public Color getBackground(AttributeSet attribute);
public Element getCharacterElement(int position);
public Font getFont(AttributeSet attribute);
public Color getForeground(AttributeSet attribute);
Search WWH ::




Custom Search