Java Reference
In-Depth Information
public Style getLogicalStyle(int position);
public Element getParagraphElement(int position);
public Style getStyle(String name);
public void removeStyle(String name);
public void setCharacterAttributes(int offset, int length, AttributeSet s,
boolean replace);
public void setLogicalStyle(int position, Style style);
public void setParagraphAttributes(int offset, int length, AttributeSet s,
boolean replace);
}
The
DefaultStyledDocument
class is the implementation of the
StyledDocument
interface
provided with the Swing components. It serves as the data model for the
JTextPane
component.
Creating a DefaultStyledDocument
You can create a
DefaultStyledDocument
in any one of the three ways listed here:
public DefaultStyledDocument()
DefaultStyledDocument document = new DefaultStyledDocument();
public DefaultStyledDocument(StyleContext styles)
StyleContext context = new StyleContext();
DefaultStyledDocument document = new DefaultStyledDocument(context);
public DefaultStyledDocument(AbstractDocument.Content content, StyleContext styles)
AbstractDocument.Content content = new StringContent();
DefaultStyledDocument document = new DefaultStyledDocument(content, context);
You can share the
StyleContext
between multiple documents or use the default context.
In addition, you can predefine the content using one of the
AbstractDocument.Content
imple-
mentations, either
GapContent
or
StringContent
. It is the responsibility of one of these
Content
implementations to store the actual
Document
content.
DefaultStyledDocument Properties
Besides having a default root element to describe the contents of the document, the
DefaultStyledDocument
makes available the style names as an
Enumeration
. These are the only
two properties defined at the
DefaultStyledDocument
level, as shown in Table 16-2. There are
other properties you can get for a
DefaultStyledDocument
; however, they require the position
or
AttributeSet
from which to get them.
Table 16-2.
DefaultStyledDocument Properties
Property Name
Data Type
Access
defaultRootElement
Element
Read-only
styleNames
Enumeration
Read-only