How It All Works
The modularity of the Swing text components can be confusing. Fortunately, most of the time it doesn’t matter how it works as long as it does work. However, some understanding of what’s going on behind the scenes is necessary for what is to come in the next four chapters.
Let’s take a look at what needs to happen for a text component to
be displayed. These behaviors describe the responsibilities of a
JTextArea
, but they are similar for
other JTextComponent
s:
The text component retrieves its UI delegate from the L&F and installs it. For
JTextArea
, this might bejavax.swing.plaf.basic.BasicTextAreaUI
.The UI delegate may set properties such as font, foreground, and selection color. The UI delegate may also set the caret, highlighter,
InputMap
s andActionMap
. The maps allow text components to respond to L&F-specific keyboard commands for actions such as cut/copy/paste, select-all, caret-to-end-of-line, page-down, and so on.The UI delegate also instantiates an
EditorKit
. ForJTextArea
this might bejavax.swing.text.DefaultEditorKit
. Most of theActions
in the text component’s array come from theEditorKit
.If the text component’s constructor didn’t receive a
Document
, it creates one.JTextArea
creates itsDocument
(aPlainDocument
) directly, but other text components delegate this to theEditorKit
.The
Document
is responsible for storing the component’s text content. It does this by breaking it into a hierarchy of one or moreElement
s. EachElement
can hold part of the ...
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.