home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Scripting / Event / EventExample.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  2.3 KB  |  88 lines

  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. //  EventExample.java
  4. //
  5. //      This example demonstrates how  a JavaBean that supports listeners
  6. //      can be used with Microsoft's Internet Explorer. This object will
  7. //      source an event called DoClick. The HTML page (Example.html) will
  8. //      sink the event and display a message box.
  9. //
  10. //  (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  11. //
  12. //////////////////////////////////////////////////////////////////////////
  13.  
  14. import java.awt.*;
  15. import java.applet.*;
  16. import java.util.*;
  17.  
  18. public class EventExample extends Applet
  19. {
  20.     private Vector m_listeners;
  21.  
  22.     /**
  23.      *  Creates a new EventExample object. It will set the layout and
  24.      *  add a button.
  25.      */
  26.     public EventExample()
  27.     {
  28.         m_listeners = new Vector();
  29.         
  30.         setLayout(new BorderLayout());
  31.         add("Center", new Button("Fire DoClick"));
  32.     }
  33.  
  34.     /**
  35.      *  Add a listener to the list of objects that are listening
  36.      *  for events.
  37.      */
  38.     public void addEventExampleListener(EventExampleListener listener)
  39.     {
  40.         if(m_listeners == null)
  41.             m_listeners = new Vector();
  42.  
  43.         m_listeners.addElement(listener);
  44.     }
  45.  
  46.     /**
  47.      *  Remove the listener for list of objects that are listening
  48.      *  for events.
  49.      */
  50.     public void removeEventExampleListener(EventExampleListener listener)
  51.     {
  52.         m_listeners.removeElement(listener);
  53.     }
  54.  
  55.     /**
  56.      *  When an action occurs that is an instance of a button,
  57.      *  fire the event.
  58.      */
  59.     public boolean action(Event event, Object obj)
  60.     {
  61.         if (event.target instanceof Button)
  62.         {
  63.             fireEvent();
  64.             return true;
  65.         }
  66.  
  67.         return false;
  68.     }
  69.  
  70.     /**
  71.      *  Iterate through the list of listener objects and fire
  72.      *  the DoClick event.
  73.      */
  74.     private void fireEvent()
  75.     {
  76.         EventExampleListener listener[];
  77.  
  78.         synchronized(m_listeners)
  79.         {
  80.             listener = new EventExampleListener[m_listeners.size()];
  81.             m_listeners.copyInto(listener);
  82.         }
  83.  
  84.         for (int i=0; i<listener.length; i++)
  85.             listener[i].DoClick();
  86.     }
  87. }
  88.