Chapter 19. Text 101

Swing provides an extensive collection of classes for working with text in user interfaces. In fact, because there’s so much provided for working with text, Swing’s creators placed most of it into its own package: javax.swing.text . This package’s dozens of interfaces and classes (plus the six concrete component classes in javax.swing) provide a rich set of text-based models and components complex enough to allow endless customization yet simple to use in the common case.

In this chapter we’ll look at JTextComponent, the base class for all of the text components shown in Figure 19-1, and then discuss JTextField, JPasswordField, and JTextArea. Then we’ll introduce what’s going on behind the scenes. We save the more complex model, event, and view classes for later, but we occasionally refer to things you may want to investigate further in the next four chapters.

A sample of Swing text components

Figure 19-1. A sample of Swing text components

JFormattedTextField is an extension of JTextField with formatting and object-parsing abilities. We’ll devote Chapter 20 to JFormattedTextField and its related classes.

Swing text components allow you to customize certain aspects of the L&F without much work. This includes the creation of custom carets (cursors), custom highlighting, and custom key bindings to associate Actions with special key combinations. These features are covered in Chapter 21.

We describe ...

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.