Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components

Using Swing's Text Components

Swing's text components display text and optionally allow the user to edit the text. Programs need text components for tasks ranging from the straightforward (enter a word and press Return) to the complex (display and edit styled text with embedded images in an Asian language).

Swing provides five text components and their supporting classes and interfaces, which meet even the most complex text requirements. In spite of their different uses and capabilities, all of Swing's text components inherit from the same superclass, JTextComponent(in the API reference documentation), which provides a highly-configurable and powerful foundation for text manipulation.

The following figure shows the JTextComponent hierarchy and places each text component class into one of three groups:

The following paragraphs describe the three groups of text components.

Group Description Swing Classes
Text Controls Also known simply as text fields, text controls can display and edit only one line of text and are action-based like buttons. Use them to get a small amount of textual information from the user and take some action after the text entry is complete. JTextField(in the API reference documentation)
and its subclass
JPasswordField(in the API reference documentation)
Plain Text Areas JTextArea, Swing's only plain text component, can display and edit multiple lines of text. Although a text area can display text in any font, all of the text is in the same font. All editing of plain text components can be accomplished through direct manipulation of the text with the keyboard and mouse, thus plain text components are typically easier to set up and use than styled text components. Also, if the length of the text is less than a few pages long, you can easily use setText and getText to modify or retrieve the component's content in a single method call. JTextArea(in the API reference documentation)
Styled Text Areas A styled text component can display and edit text using more than one font. Some styled text components allow embedded images and even embedded components. These text components are powerful and multi-faceted components suitable for high-end needs. You typically have to do more programming up front to set up and use styled text components because much of their functionality is not available through direct manipulation with the mouse and keyboard. For example, to support text style editing, you normally have to create a user interface. One handy, easy-to-use feature provided by JEditorPane makes editor panes and text panes particularly useful for displaying uneditable help information: they can be loaded with formatted text from a URL. JEditorPane(in the API reference documentation)
and its subclass
JTextPane(in the API reference documentation)

The next section, An Example of Using Each Text Component, shows an application that creates one of each of Swing's text components and describes the application's code. By studying this example you can learn the basics of creating and using each text component. Many programmers and programs will be served with this basic information. However, you will have scratched only the surface of Swing's text API. The remaining sections about the text components are here to help you navigate the waters.


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components