O'Reilly logo

Learning Java, 3rd Edition by Jonathan Knudsen, Patrick Niemeyer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required