Java Reference
In-Depth Information
listener.actionPerformed(event);
As a result, the message is printed.
You can think of the
actionPerformed
method as another example of a callback,
similar to the
measure
method of the
Measurer
class. The windowing toolkit calls
the
actionPerformed
method whenever the button is pressed, whereas the
DataSet
calls the
measure
method whenever it needs to measure an object.
You can test this program out by opening a console window, starting the
ButtonViewer
program from that console window, clicking the button, and
watching the messages in the console window (see
Figure 3
).
409
410
Figure 3
Implementing an Action Listener
ch09/button1/ButtonViewer.java
1
import
java.awt.event.ActionListener;
2
import
javax.swing.JButton;
3
import
javax.swing.JFrame;
4
5 /**
6
This program demonstrates how to install an action listener.
7 */
8
public class
ButtonViewer
9 {
10
public static void
main(String[] args)