Java Reference
In-Depth Information
CHAPTER 15
■ ■ ■
Basic Text Components
C
hapter 14 explored the dynamic input selection control offered with the
JSpinner
of the
Swing component set. In this chapter, you will look at the basic capabilities of the Swing text
components. The more advanced text component capabilities are covered in the next chapter.
The Swing component set features five text components. They all share a common parent
class,
JTextComponent
, which defines the common behavior for all text controls.
The direct subclasses of
JTextComponent
are
JTextField
,
JTextArea
, and
JEditorPane
.
JTextField
is used for a single line of single-attributed text (that is, a single font and a single
color).
JTextField
has a single subclass,
JPasswordField
, for when a
JTextField
needs to be
used with an input mask for the entry of a password.
JTextArea
is used for multiple lines of
single-attributed text input.
JEditorPane
is a generic editor that can support the editing of
multiple-attributed input. Its subclass
JTextPane
is customized for input in plain-text style.
In both cases, the input can be images as well as components, in addition to text.
Overview of the Swing Text Components
Like all other Swing components, text components live in an MVC world. The components
shown in Figure 15-1, which is a class hierarchy diagram, are the various available UI delegates.
The remaining part of the UI delegate model is the text view, which is based on the
View
class
and discussed further in Chapter 16.
■
Note
All
JTextComponent
subclasses are in the
javax.swing
package. With the exception of
the event-related pieces, the support interfaces and classes discussed in this chapter are found in the
javax.swing.text
package (or a subpackage). The Swing-specific, text-related event pieces are found
in the
javax.swing.event
package, with remaining bits in
java.awt.event
and
java.beans
.
The data model for each of the components is an implementation of the
Document
interface, of
which there are five extensions (or implementations). The single-attributed components use
the
PlainDocument
class as their data model, while the multiple-attributed components use
DefaultStyledDocument
as their model. Both of these classes subclass the
AbstractDocument
class,
which defines their common
Document
interface implementation. The
DefaultStyledDocument
class also implements the
StyledDocument
interface, which is an extension of
Document
for
supporting multiple-attributed content. An additional
Document
implementation,
HTMLDocument
,
521