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. 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 JTextPane in Chapter 22 and discuss styles, the Document model and Views. Style features include structured text supporting multiple fonts and colors, and even embedded Icons and Components. Finally, we turn to JEditorPane in Chapter 23 and see how all of this is tied together by something called an EditorKit. EditorKits allow you to define which view objects should be used, which special actions your editor will support, and how your documents can be input and output via streams. You can even register EditorKits for specific content types to enable JEditorPanes to handle those content types automatically. |