Chapter 17. 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 fundamental pieces work. You should understand what components and containers are, how they work together 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 on this topic 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 ...

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.