Java user-defined event listener complete example
Java user-defined event listener complete example--sunfruit many of the user-defined events are not examples, or examples are incomplete, the following is a complete example, and write a comment for reference, the complete instance source code is as follows:
Package demo;
Import Java.util.EventObject;
/**
* Title: Event Processing class, inheriting the event base class
* Description:
* Copyright:copyright (c) 2005
* Company:cuijiang
* @author not attributable
* @version 1.0
*/
public class Demoevent extends EventObject
{
Private Object obj;
Private String SName;
Public demoevent (Object source,string sName) {
Super (source);
obj = source;
This.sname=sname;
}
Public Object GetSource ()
{
return obj;
}
public void Say ()
{
System.out.println ("This is the Say method ...");
}
Public String GetName ()
{
return sName;
}
}
Package demo;
Import Java.util.EventListener;
/**
* Title: Listener interface
* Description:
* Copyright:copyright (c) 2005
* Company:cuijiang
* @author not attributable
* @version 1.0
*/
Public interface Demolistener extends eventlistener{
public void Demoevent (demoevent DM);
}
Package demo;
Import java.util.*;
/**
* Title: Class using the event
* Description: This class implements the addition of listeners and the execution of listener methods, and implements events due to property changes
* Description: Be aware of synchronization issues when adding, deleting, and executing listeners
* Copyright:copyright (c) 2005
* Company:cuijiang
* @author not attributable
* @version 1.0
*/
public class demosource{
Private vector repository = new vector ();
Private Demolistener DL;
Private String sname= "";
Public Demosource ()
{
}
Register the Listener, if you use ArrayList instead of vectors, pay attention to synchronization issues
public void Adddemolistener (Demolistener dl)
{
Repository.addelement (DL);//This step should pay attention to the synchronization problem
}
If you use ArrayList instead of vectors, be aware of synchronization issues
public void Notifydemoevent (Demoevent event) {
enumeration enum = Repository.elements ();//This step should pay attention to the synchronization problem
while (Enum.hasmoreelements ())
{
DL = (Demolistener) enum.nextelement ();
Dl.demoevent (event);
}
}
Delete the listener, if you are using ArrayList instead of vectors, pay attention to the synchronization problem
public void Removedemolistener (Demolistener dl)
{
Repository.remove (DL);//This step should pay attention to the synchronization problem
}
/**
* Set Properties
* @param str1 String
*/
public void SetName (String str1)
{
Boolean bool=false;
if (str1==null && sname!=null) bool=true;
else if (str1!=null && sname==null) bool=true;
else if (!sname.equals (str1)) bool=true;
THIS.SNAME=STR1;
Execute event If change occurs
if (bool) notifydemoevent (new Demoevent (This,sname));
}
Public String GetName ()
{
return sName;
}
}
Package demo;
Import Java.lang.Thread;
/**
* Title: Test class
* Description: Tested for events caused by changing attributes
* Copyright:copyright (c) 2005
* Company:cuijiang
* @author not attributable
* @version 1.0
*/
public class Testdemo
Implements Demolistener {
Private Demosource DS;
Public Testdemo ()
{
Ds=new Demosource ();
Ds.adddemolistener (this);
SYSTEM.OUT.PRINTLN ("Add listener Complete");
try {
Thread.Sleep (3000);
Changing properties, triggering events
Ds.setname ("Change attribute, trigger event");
}
catch (Interruptedexception ex) {
Ex.printstacktrace ();
}
Ds.adddemolistener (this);
SYSTEM.OUT.PRINTLN ("Add listener complete 2″);
try {
Thread.Sleep (3000);
Changing properties, triggering events
Ds.setname ("Change attribute, trigger event 2″);
}
catch (Interruptedexception ex) {
Ex.printstacktrace ();
}
Ds.removedemolistener (this);
SYSTEM.OUT.PRINTLN ("Add listener complete 3″);
try {
Thread.Sleep (3000);
Changing properties, triggering events
Ds.setname ("Change attribute, trigger event 3″);
}
catch (Interruptedexception ex) {
Ex.printstacktrace ();
}
}
public static void Main (String args[])
{
New Testdemo ();
}
/**
* Demoevent
*
* @param DM demoevent
* @todo Implement this test. Demolistener method
*/
public void Demoevent (Demoevent dm) {
SYSTEM.OUT.PRINTLN ("Event handling method");
System.out.println (Dm.getname ());
Dm.say ();
}
}
Sample code for Java custom listeners