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 JTextComponents:
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,
InputMaps 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. ForJTextAreathis might bejavax.swing.text.DefaultEditorKit. Most of theActionsin the text component’s array come from theEditorKit.If the text component’s constructor didn’t receive a
Document, it creates one.JTextAreacreates itsDocument(aPlainDocument) directly, but other text components delegate this to theEditorKit.The
Documentis responsible for storing the component’s text content. It does this by breaking it into a hierarchy of one or moreElements. EachElementcan hold part of the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access