An example of the Java event handling mechanism (custom events)

  • 2020-05-26 08:35:09
  • OfStack

Java event handling mechanism

The participants of the event mechanism in java have three roles:

1.event object: event state object, which is used in the corresponding listener methods. As a parameter, 1 exists in the listerner methods

2.event source: the specific event source, for example, if you click on an button, button is event source. To make button respond to certain events, you need to register a specific listener.

3.event listener: for each specific event, a specific Java method is defined accordingly. These methods are centrally defined in the event listener (EventListener) interface, which inherits java.util.EventListener. A class that implements some or all of the methods in the event listener interface is an event listener.

Along with the occurrence of an event, the corresponding state is usually encapsulated in the event state object, which must be inherited from java.util.EventObject. The event state object is passed as a single argument to the listener method that should respond to the event. The event source that emits a particular event is identified as defining a registration method for the event listener in a specified design format and accepting a reference to the specified event listener interface instance.

Specific to the event class it is listening to, when it is listening to event object generation, it will call the corresponding method to handle.

First look at the event package offered by jdk:

public interface EventListener: the tag interface that all event listener interfaces must extend.

public class EventObject extends Object implements Serializable

All event state objects will derive from its root class. All Event are constructed with references to the object "source", which logically is considered the object where the Event connection originated.

(1) create the DoorEvent class through the DoorEvent.java file, which inherits EventObject.


/**
*  Define an event object that must be inherited EventObject
*/
public class DoorEvent extends EventObject {

 private static final long serialVersionUID = 6496098798146410884L;

 private String doorState = "";//  The door is open and closed 

 public DoorEvent(Object source, String doorState) {
  super(source);
  this.doorState = doorState;
 }

 public void setDoorState(String doorState) {
  this.doorState = doorState;
 }

 public String getDoorState() {
  return this.doorState;
 }

}

(2) define a new event listening interface, which is inherited from EventListener; This interface contains handlers for doorEvent events:


/**
*  Define the listening interface, which is responsible for listening DoorEvent The event 
*/

public interface DoorListener extends EventListener {
  public void doorEvent(DoorEvent event);
}

Through the interface above, we define the event listener class, which implements the listening function and event handling function.


/**
*  The class for   The door 1 Monitor the implementation of the interface, do specific open the door, close the door action 
*/


public class DoorListener1 implements DoorListener {
 @Override
 public void doorEvent(DoorEvent event) {
  // TODO Auto-generated method stub
  if (event.getDoorState() != null && event.getDoorState().equals("open")) {
   System.out.println(" The door 1 Open the ");
  } else {
   System.out.println(" The door 1 Shut down ");
  }
 }

}

/**

*  The class for   The door 2 Monitor the implementation of the interface, do the specific open the door, close the door, as well as light, light off action 
*/


public class DoorListener2 implements DoorListener {

 @Override
 public void doorEvent(DoorEvent event) {
  // TODO Auto-generated method stub
  if (event.getDoorState() != null && event.getDoorState().equals("open")) {
   System.out.println(" The door 2 Turn on, and turn on the hallway lights at the same time ");
  } else {
   System.out.println(" The door 2 Turn off, and turn off the lights in the hallway ");
  }
 }

}

(3) create an event source class through DoorManager.java, which USES an Collection listeners object to store all event listener objects through addDoorListener(..) This way. notifyListeners (..) Is a method that triggers an event to notify the system that an event has occurred and that you call the appropriate handler.


/**
*  The event source object, in this case you can think of it as 1 A remote control for opening and closing doors, 
* ( If it is in swing Medium, just like that 1 a button)
*/


public class DoorManager {
 private Collection listeners;

 /**
  *  Add event 
  * 
  * @param listener
  *   DoorListener
  */
 public void addDoorListener(DoorListener listener) {
  if (listeners == null) {
   listeners = new HashSet();
  }
  listeners.add(listener);
 }

 /**
  *  Remove event 
  * 
  * @param listener
  *   DoorListener
  */
 public void removeDoorListener(DoorListener listener) {
  if (listeners == null)
   return;
  listeners.remove(listener);
 }

 /**
  *  Trigger door opening event 
  */
 protected void fireWorkspaceOpened() {
  if (listeners == null)
   return;
  DoorEvent event = new DoorEvent(this, "open");
  notifyListeners(event);
 }

 /**
  *  Trigger closing event 
  */
 protected void fireWorkspaceClosed() {
  if (listeners == null)
   return;
  DoorEvent event = new DoorEvent(this, "close");
  notifyListeners(event);
 }

 /**
  *  Notify all DoorListener
  */
 private void notifyListeners(DoorEvent event) {
  Iterator iter = listeners.iterator();
  while (iter.hasNext()) {
   DoorListener listener = (DoorListener) iter.next();
   listener.doorEvent(event);
  }
 }
}

(4) ok, finally write a test program test 1 under our custom event, this section of the program should not be difficult to understand :)


/**
*  The main program, you imagine, is the person opening the door 
*/


public class DoorMain {
 public static void main(String[] args) {
  DoorManager manager = new DoorManager();
  manager.addDoorListener(new DoorListener1());//  To the door 1 Add listener 
  manager.addDoorListener(new DoorListener2());//  To the door 2 Add listener 
  //  Open the door 
  manager.fireWorkspaceOpened();
  System.out.println(" I have come in ");
  //  close the door 
  manager.fireWorkspaceClosed();
 }
}

Run DoorMain

1 open the door
Door 2 opens and the hallway light is turned on

I have come in

1 shut down the door
Door 2 is closed and hallway lights are turned off

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: