Learning Java 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, A First Application
(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( ) is the event itself.
There's one more thing worth saying about buttons, which applies
to any component that generates an action event. Java lets us
specify an "action command" string for buttons (and other
components, like menu items, that can generate action events).
The action command is less interesting than it sounds. It is just
a String that serves to identify the component that sent the
event. By default, the action command of a JButton
is the same as its label; it is included in action events, so you
can use it to figure out which button an event came from.
To get the action command from an action event, call the event's
getActionCommand( ) method. The following code
checks whether the user pressed the Yes button:
public void actionPerformed(ActionEvent e){
if (e.getActionCommand( ).equals("Yes") {
//the user pressed "Yes"; do something
...
}
}
You can change the action command by calling the button's setActionCommand( ) method. The following code changes button myButton's action command to "confirm":
myButton.setActionCommand("confirm");
It's a good idea to get used to setting action commands
explicitly; this helps to prevent your code from breaking when
you or some other developer "internationalizes" it, or otherwise
changes the button's label. If you rely on the button's label,
your code will stop working as soon as that label changes; a
French user might see the label Oui rather than
Yes. By setting the action command, you eliminate
one source of bugs; for example, the button myButton
in the previous example will always generate the action command
confirm, regardless of what its label says.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|