Programming and Problem Solving with Java

(やまだぃちぅ) #1
8.3 Event Handling | 387

The call to its constructor includes the string that should appear inside the button. For example:


done = new JButton("Done"); // Create a JButton object


The call to the addmethod for a content pane called dataPaneis written as follows:


dataPane.add(done);


When the setVisiblemethod for the frame is called, a button is included in the frame
with the word “Done” appearing inside it. Note that we use our convention of starting a vari-
able identifier with a lowercase letter, but the string that appears in the button is capitalized.
Java may not use proper English capitalization, but our user interface should! The identifier
donewith a lowercase “d” is the name of a button object; “Done” with a capital “D” is the string
that appears in the button on the screen. Here’s a code segment showing the steps we’ve cov-
ered so far:


JButton done; // Declare a button variable


done = new JButton("Done"); // Instantiate a button
... // Register the event listener
outPane.add(done); // Add the button to the frame


This segment would appear in main, or wherever we are creating the user interface. We still
need to look at how to register the event listener. First, however, we have to create one.


Creating and Registering a Button Event Listener


First we must declare a new class (a button event listener) to handle button events. This class
is based on ActionListener, which we will describe shortly. The button event listener is regis-
tered with an event source through a method call. The method to register the event listener is
called addActionListener, and it takes an object of our listener class as its argument.
We’ve already defined numerous classes in Java. However,ActionListeneris
a bit different. Rather than designing the class heading and methods ourselves,
Java gives us a precise model to follow. This Java-provided model is called an in-
terface. Previously, we defined an interfacein the general sense. In Java, however,
an interface is part of the language; it is a way of specifying the fields and meth-
ods that must be present in a class that is an implementationof the interface.
Likewise, we previously defined implementationas the stage in the software de-
velopment life cycle in which an algorithm is translated into a programming lan-
guage. In Java, an implementation is a specific part of the language. The Java
designers have used these general terms in a very specific way to define part of
the language.
The ActionListenerinterface specifies that we need to write a class that has one method
called actionPerformed, with one parameter that takes the event source object as an argument.
Here is the code for a class that implements the ActionListenerinterface:


Interface (in Java) A model for
a class that specifies the fields
and methods that must be pres-
ent in a class that implements
the interface
Implementation (in Java) A
class containing the definitions
of the methods specified in an
interface
Free download pdf