11.2. Handling Events by Implementing a Listener Interface

In the previous example, the listener object was completely separate from the applet that used it. Although in some cases such a separation promotes object reuse, in this case the separation made it more difficult for the event listener to call the applet's methods. First of all, the event handler had to use the somewhat awkward approach of calling getSource and performing a typecast, just to obtain a reference to the applet. Second, this reference only enabled the listener to call public methods, not private or protected ones.

An alternative to this approach is for the applet to register itself as the mouse event listener. In order for this approach to comply with Java's strict typing ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.