The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
This section provides background information you might need when using Swing text components. If you intend to use an unstyled text component a text field, password field, formatted text field, or text area go to its how-to page and return here only if necessary. If you intend to use a styled text component, see How to Use Editor Panes and Text Panes, and read this section as well. If you do not know which component you need, read on.
Swing 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 Enter) to the complex (display and edit styled text with embedded images in an Asian language).
Swing provides six text components, along with supporting classes and interfaces that meet even the most complex text requirements. In spite of their different uses and capabilities, all Swing text components inherit from the same superclass,
JTextComponent
, which provides a highly-configurable and powerful foundation for text manipulation.
The following figure shows the JTextComponent
hierarchy.
The following picture shows an application called TextSamplerDemo
that uses each Swing text component.
setEditable
.The TextSamplerDemo
example uses the text components in very basic ways. The following table tells you more about what you can do with each kind of text component.
Group | Description | Swing Classes |
---|---|---|
Text Controls | Also known simply as text fields, text controls can display only one line of editable text. Like buttons, they generate action events. Use them to get a small amount of textual information from the user and perform an action after the text entry is complete. |
JTextField and its subclasses
JPasswordField and
JFormattedTextField |
Plain Text Areas | JTextArea can display multiple lines of editable text. Although a text area can display text in any font, all of the text is in the same font. Use a text area to allow the user to enter unformatted text of any length or to display unformatted help information. |
JTextArea |
Styled Text Areas | A styled text component can display editable text using more than one font. Some styled text components allow embedded images and even embedded components. Styled text components are powerful and multi-faceted components suitable for high-end needs, and offer more avenues for customization than the other text components.
Because they are so powerful and flexible, styled text components typically require more initial programming to set up and use. One exception is that editor panes can be easily loaded with formatted text from a URL, which makes them useful for displaying uneditable help information. |
JEditorPane and its subclass JTextPane |
This Tutorial provides information about the foundation laid by the JTextComponent
class and tells you how to accomplish some common text-related tasks.
To learn more about text components in JavaFX, see the Using Text and Text Effects in JavaFX and Using JavaFX UI Controls: Text Field tutorials.