Installing ActionListeners
The ActionListener
instance to receive ActionEvents
in the program example of the previous section is the class WombatEventHandlingExample
itself. This is defined during the class declaration by the argument "implements ActionListener
". To satisfy the requirements of an ActionListener
instance, the method onActionEvent(ActionEvent event)
has to be implemented. The following sections were taken from the source code listing and show the relevant segments for successfully installing an ActionListener
.
| ... |
| 16 public class WombatEventHandlingExample |
| 17 extends Frame implements ActionListener { |
| ... |
| 31 // add the ActionListener |
| 32 b1.setActionListener(this); |
| 33 button_right.setActionListener(this); |
| ... |
| 52 public void onActionEvent(ActionEvent event) { |
| ... |
For onActionEvent
s to be triggerd by both Button
s, we pass a pointer to the class instance by using Button.setActionListener(this)
and therewith register the ActionListener
. A pointer to any other class that implements the interface ActionListener
will be acceptable as well.