The Document Model
The Document
is the M
part of the MVC (Model-View-Controller) architecture for all of
Swing’s text components. It is responsible for the text content of the
component as well as relevant style information for text components that
support styled text. The Document
model must be simple enough to be used by JTextField
, but powerful and flexible enough
to be used by JTextPane
and JEditorPane
. Swing accomplishes this by
providing the classes and interfaces shown in Figure 22-7.
Figure 22-7. High-level Document class diagram
Basically, a Document
partitions its content into small pieces called Element
s. Each Element
is small enough that its style
information can be represented by a single AttributeSet
. The Element
s are organized into a tree
structure[22] with a single root.
Swing provides the Document
interface, which doesn’t support styled text, and the StyledDocument
interface, which does. But note
that there is no StyledElement
interface. Swing provides a single Element
interface that does support style. The
simpler document types (such as PlainDocument
, which JTextField
and JTextArea
use by default) use Element
s but don’t assign any style
information to them.
The Element Interface
The Element
interface
is used to describe a portion of a document. But note that an Element
does not actually
contain a portion of the document; it just defines a way of structuring a portion. ...
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.