Java event mechanism

Source: Internet
Author: User
Java Event mechanism (continued)-- Before a custom event starts, you must first ask: Are you familiar with Java. util. eventobject and Java. util. eventlistener classes and their existing subclasses? If you are already familiar with the event listeners that JDK provides for us and are familiar with the events that JDK has prepared for us, such as mouseevent, keyevent, and javaswevent, you must have an understanding of Java's event mechanism. However, you may still feel that it is okay to use it, but the principle is still a bit confused. Then we will implement these events and listeners on our own, and we will name this custom event. In fact, custom events are very useful in Java. Sometimes we want our programs to generate an event, but we don't want (or can't) to use the mouse, keyboard and other input devices, for example, if you write an application, in this program, once you receive the email, the relevant processing of the mail, for the "received mail" event, JDK is not defined. For such events and listeners of such events, we can only do it by ourselves. Next we will start our "Innovation" process with an instance: first, we need to clarify the resources required in JDK: Class eventobject as the parent class to generate our own event class, the eventlistener interface is used to implement our own listeners. The rest is how to register these events and test them. Let's implement it step by step: (1) create a demoevent class through the demoevent. Java file. This class inherits eventobject. The parameters of the constructor of this class pass the event source that generates this event (such as various controls). The getsource method is used to obtain the reference of this event source. Demoevent. Java
Package demo. listener; Import java. util. eventobject; Public class demoevent extends eventobject{Object OBJ;Public demoevent (Object source){Super (source );OBJ = source;}Public object getsource (){Return OBJ;}Public void say (){System. Out. println ("this is say method ...");}}
(2) define a new event listening interface that inherits from eventlistener. This interface contains a handler for demeevent events: Demolistener. Java
Package demo. listener; Import java. util. eventlistener; Public interface demolistener extends eventlistener{Public void demoevent (demoevent DM );}
Through the above interface, we define the event listening class, which specifically implements the listening and event processing functions. Let's look back at the four implementation methods mentioned above. Aren't we using the third method-the external class writing method? Listener1.java
Package demo. listener; Public class listener1 implements demolistener{Public void demoevent (demoevent de){System. Out. println ("Inside listener1 ...");}}

Listener2.java

Package demo. listener; Public class listener2 implements demolistener{Public void demoevent (demoevent de){System. Out. println ("Inside listener2 ...");}}

Listener3.java
Package demo. listener; Public class listener3 implements demolistener{Public void demoevent (demoevent de){System. Out. println ("Inside listener3 ...");}}
(3) Use demesource .. the Ava file creates an event source class, which uses a java. utile. vector object to store all event listener objects. The storage method is through addlistener (..) this method. Yydemeevent (...) is the method used to trigger the event. It is used to notify the system that the event has occurred. You can call the corresponding processing function (callback function. Demosource. Java
 Package demo. listener;Import java. util .*; Public class demosource{Private vector repository = new vector ();Demolistener dl;Public demosource (){ }Public void adddemolistener (demolistener dl){Repository. addelement (DL );}Public void policydemoevent (){Enumeration Enum = repository. Elements ();While (enum. hasmoreelements ()){DL = (demolistener) enum. nextelement ();DL. demoevent (New demoevent (this ));}}} 
(4) Okay. Finally, write a test program to test our custom events. This program should be easy to understand :) Testdemo. Java
 Package demo. listener; Public class testdemo{Demosource Ds; Public testdemo (){Try {DS = new demosource ();Listener1 L1 = new listener1 ();Listener2 L2 = new listener2 ();Listener3 l3 = new listener3 (); DS. adddemolistener (L1 );DS. adddemolistener (L2 );DS. adddemolistener (L3 ); DS. notifydemoevent (); } Catch (exception ex){Ex. printstacktrace ();}} Public static void main (string ARGs []){New testdemo ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.