Java Programming/Applets/Event Listeners

An Event Listener, once set to an applet object, waits for some action to be performed on it, be it mouse click, mouse hover, pressing of keys, click of button, etc. The class you are using (e.g., etc.) reports the activity to a class set by the class using it. That method then decides on how to react because of that action, usually with a series of if statements to determine which action it was performed on. will return the name of the object that the event was performed on, while the source is the object passed to the function when the action is performed. Every single time the action is performed, it calls the method.

ActionListener
is an interface that could be implemented in order to determine how a certain event should be handled. When implementing an interface, all methods in that interface should be implemented,  interface has one method to implement named.

The code listing 9.6 shows how to implement :

When you compile and run the above code, the message "Hi!!!" will appear when you click on the button.

MouseListener
Applet mouse listener does not differ from the AWT mouse listener in general. When the mouse is in the applet area, the listener receives notifications about the mouse clicks and drags (if  is registered) and mouse movements (if   is registered). As applets are often small, it is a common practice to let applet itself to implement the mouse listeners.