Chapter 15. 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. The chapter ends with a brief description of how to implement your own components in Swing.
Text Components
Swing gives us sophisticated text components, from plain text entry boxes to HTML interpreters. For full coverage of Swing’s text capabilities, see Java Swing, by Robert Eckstein, Marc Loy, and Dave Wood (O’Reilly & Associates). In that encyclopedic book, six 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:
JTextArea
is a multiline text editor;
JTextField is a simple, single-line 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access