Java Reference
In-Depth Information
ActionEvent . This class is also found in the AWT library (in java.awt.events ).
An object of type ActionEvent contains information about the event noticed by
the runtime system.
For our counter application we implement a listener in the class CounterLis-
tener .A CounterListener implements the Java interface ActionListener . The
implementation requires the definition of only one method actionPerformed :
public void actionPerformed(ActionEvent evt)
This is the method called by the runtime system whenever the button is pressed.
Youdonot have to call this method yourself, and you should not do this. The
!
runtime system also generates an action event evt and passes it to the method
actionPerformed . The programmer puts the code into the body of method ac-
tionPerformed 1 . This code is then executed in response to pressing the button.
The information contained in the action event can be used to gather further in-
formation on what triggered the event.
The code for the class CounterListener is listed below. We shall explain it
after the listing.
File: its/CounterGUI/CounterListener.java
1. package its.CounterGUI;
2.
3. import java.awt.event.ActionListener;
4. import java.awt.event.ActionEvent;
5.
6. public class CounterListener implements ActionListener{
7.
8.
private CounterPanel countPane;
9.
10.
public CounterListener(CounterPanel counp) {
11.
countPane = counp;
12.
}
13.
14. // This method is called by the runtime system.
15. // The programmer has to add the code to be executed
16. // as a response to the event.
17.
public void actionPerformed(ActionEvent evt){
18.
// Beginning of own code
19.
String actionCommand = evt.getActionCommand();
20.
if (actionCommand.equals("Up")){
21.
countPane.increment();
22.
}
23.
else if (actionCommand.equals("Down")){
24.
countPane.decrement();
1
This formulation has to be taken with a grain of salt. Some of the code might actually be in
other methods which are then called from inside actionPerformed .
Search WWH ::




Custom Search