Chapter 18. More Swing Components

In the previous chapter, we described most of the components that Swing offers for building user interfaces. In this chapter, you’ll find out about the rest. These include Swing’s text components, trees, and tables. These types of components have considerable depth but are quite easy to use if you accept their default options. We’ll show you the easy way to use these components and start to describe the more advanced features of each. Later in this chapter, we’ll also give an example of how to implement your own, custom components in Swing.

Text Components

Swing offers sophisticated text components, from plain-text entry boxes to HTML interpreters. For full coverage of Swing’s text capabilities, see O’Reilly’s Java Swing. In that encyclopedic book, several meaty chapters are devoted to text. It’s a huge subject; we’ll just scratch the surface here.

Let’s begin by examining the simpler text components. JTextField is a single-line text editor and JTextArea is a simple, multiline text editor. Both JTextField and JTextArea derive from the JTextComponent class, which provides the functionality they have in common. This includes methods for setting and retrieving the displayed text, specifying whether the text is “editable” or read-only, manipulating the cursor position within the text, and manipulating text selections.

Observing changes in text components requires an understanding of how the components implement the Model-View-Controller (MVC) architecture. ...

Get Learning Java, 3rd 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.