Chapter 14. Using Swing Components
In the previous chapter, we discussed a number of concepts, including how Java’s user interface facility is put together and how the larger pieces work. You should understand what components and containers are, how you use them to create a display, what events are, how components use them to communicate with the rest of your application, and what layout managers are.
Now that we’re through with the general concepts and background, we’ll get to the fun stuff: how to do things with Swing. We will cover most of the components that the Swing package supplies, how to use these components in applets and applications, and how to build your own components. We will have lots of code and lots of pretty examples to look at.
There’s more material than fits in a single chapter. In this chapter, we’ll cover all the basic user interface components. In the next chapter, we’ll cover some of the more involved topics: text components, trees, tables, and creating your own components.
Buttons and Labels
We’ll start
with the simplest
components: buttons and labels.
Frankly, there isn’t much to say about them. If you’ve
seen one button, you’ve seen them all; and you’ve already
seen buttons in the applications in Chapter 2
(HelloJava3 and HelloJava4). A
button generates
an
ActionEvent when the
user presses it. To receive these events, your program registers an
ActionListener
, which must implement
the
actionPerformed( )
method. The argument passed to actionPerformed( ...
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