Alternate Patterns for Events

Since ActionListener is an interface and not a class, it can be implemented wherever is convenient. For example, an applet can handle its own events like this:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class BeepApplet extends Applet implements ActionListener {
  
   public void init () {
   
     // Construct the button
     Button beep = new Button("Beep");

     // add the button to the layout
     this.add(beep);

     // specify that action events sent by this
     // button should be handled by the applet itself
     beep.addActionListener(this);
   
   }

  public void actionPerformed(ActionEvent e) {

    Toolkit.getDefaultToolkit().beep();

  }

}
The main benefit of this event model is that the GUI can be separated from the code.
Previous | Next | Top | Cafe au Lait

Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 5, 1997