Handling User Events

When a user event is generated by a component that has a listener, a method will be called automatically. The method must be found in the class that was specified when the listener was attached to the component. For instance, in the example of the fireTorpedos object, the method must be located in the same program because the this statement was used.

Each listener has different methods that are called to receive their events. The ActionListener interface sends events to a class called actionPerformed(). The following is a short example of an actionPerformed() method:

public void actionPerformed(ActionEvent evt) {
    // method goes here

All action events sent in the program will go to this method. If only one component in ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.