JAVA user custom event listener instance code
- 2020-06-23 00:32:24
- OfStack
JAVA user custom event listener instance code
Many user-defined events are introduced without examples, or examples are incomplete, the following is a complete example, and written comments for reference, complete example source code is as follows:
package demo;
import Java.util.EventObject;
/**
* Title: Event handler class, which inherits 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 a say methods ...");
}
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: Classes that use events
* Description: This class implements the addition of listeners and the execution of listener methods, and the execution of events due to property changes
* Description: Pay attention to synchronization 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 listeners if not used here Vector It USES ArrayList So pay attention to synchronization
public void addDemoListener(DemoListener dl)
{
repository.addElement(dl);// Pay attention to synchronization in this step
}
// If it's not used here Vector It USES ArrayList So pay attention to synchronization
public void notifyDemoEvent(DemoEvent event) {
Enumeration enum = repository.elements();// Pay attention to synchronization in this step
while(enum.hasMoreElements())
{
dl = (DemoListener)enum.nextElement();
dl.demoEvent(event);
}
}
// Delete listener if not used here Vector It USES ArrayList So pay attention to synchronization
public void removeDemoListener(DemoListener dl)
{
repository.remove(dl);// Pay attention to synchronization in this step
}
/**
* 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;
// If the change is made, the event is executed
if(bool) notifyDemoEvent(new DemoEvent(this,sName));
}
public String getName()
{
return sName;
}
}
package demo;
import java.lang.Thread;
/**
* Title: The test class
* Description: Test the occurrence of an event caused by a property change
* 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(" Finished adding listeners ");
try {
Thread.sleep(3000);
// Change attributes , Triggering event
ds.setName(" Change attributes , Triggering event ");
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
ds.addDemoListener(this);
System.out.println(" Finished adding listeners 2");
try {
Thread.sleep(3000);
// Change attributes , Triggering event
ds.setName(" Change attributes , Triggering event 2");
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
ds.removeDemoListener(this);
System.out.println(" Finished adding listeners 3");
try {
Thread.sleep(3000);
// Change attributes , Triggering event
ds.setName(" Change attributes , Triggering 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();
}
}
Thank you for reading, I hope to help you, thank you for your support to this site!