Multiple ActionListeners

You aren't limited to just one listener per event, either. For example, to beep five times when the button is pressed,

import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class BeepFiveApplet extends Applet  {
  
   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 a new BeepAction object
     beep.addActionListener(new BeepAction());
     beep.addActionListener(new BeepAction());
     beep.addActionListener(new BeepAction());
     beep.addActionListener(new BeepAction());
     beep.addActionListener(new BeepAction());
   
   }

}
When the Button beep fires an ActionEvent, each of the five BeepAction objects receives a copy of the event. The order they receive it in is not specified.


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