Java Custom listening event

Source: Internet
Author: User

Due to work needs, I recently studied the Java event listening mechanism. Sometimes you need to customize event listening to complete specific tasks. The following describes how to create and use a Custom Event listener.

(In fact, there are already many examples written on the Internet. This time, I just recorded my learning experiences)

Participants of the event mechanism in Java have three roles:

1. event object: it is the specific "Event" when an event is generated. It is used in the corresponding listener method. As a parameter, it is generally stored in the listerner method.

2. event Source: the entity that accepts the event. For example, if you click a button, the button is the event source. In this way, you must make the button respond to some events, you need to register a specific listener, such as the mouseclicked method in the mouseevent. Then it must have the add method.

3. Event listener: Specifies the event class for listening. When the corresponding event object is generated, it calls the corresponding method for processing. In Windows programming, this kind of response is implemented using the callback mechanism.

First, define the event to inherit the java. util. eventobject class. (JDK uses 1.6)

Public class runningmevent extends eventobject {

/**
* Serialization version number
*/
Private Static final long serialversionuid = 1l;

Private object objsource;
Private object message;

Public runperformevent (Object source, object message ){
Super (source );
// Todo auto-generated constructor stub
This. objsource = source;
This. Message = message;
}

Public object getobjsource (){
Return objsource;
}

Public void setobjsource (Object objsource ){
This. objsource = objsource;
}

Public object getmessage (){
Return message;
}

Public void setmessage (Object message ){
This. Message = message;
}
}

Second, define the event listening implementation java. util. eventlistener, including the processing of runningmevent events

Public interface irunperformeventlistener extends eventlistener {
Void runmessagechanged (runperformevent event); // custom Implementation Method
}

Third, define the event listening management class to add, delete, and start listeners.

Import java. util. arraylist;
Import java. util. List;
Import com. SZL. listenner. iface. irunlistener meventlistener;
Import com. SZL. listenner. model. run=mevent;

Public class managerlistener {

Private list <iruninclumeventlistener> listeners = NULL;

Public managerlistener ()
{
This. Listeners = new arraylist <iruninclumeventlistener> ();
}

// Add a listener
Public void addrunlistener meventlistener (irunlistener meventlistener E)
{
This. listeners. Add (E );
}

// Delete a listener

Public void deleterunlistener meventlistener (irunlistener meventlistener E)
{
This. listeners. Remove (E );
}

// Activate the listener event
Public void fireruninclumeventlistener (runningmevent event)
{
For (irunlistener meventlistener listener: This. Listeners)
{
Listener. runmessagechanged (event );
}
}
}

Step 4: Add listening events and call the test

Public class useeventlistener {

Managerlistener mL;
Public useeventlistener ()
{
ML = new managerlistener ();
ML. addrunperformeventlistener (New eventinterface (); // Add a Custom Event
}

// Method for executing custom events
Public void testlistener (string Str ){
ML. fireruninclumeventlistener (New runningmevent (this, STR); // This indicates this class, which injects this class into the listener.
}

// Internal class for listener
Private class eventinterface implements iruninclumeventlistener
{
Public void runmessagechanged (runperformevent event ){
// Todo auto-generated method stub
Do_runmessagechanged_actionevent (event );
}
}

// Trigger a Custom Event
Protected void do_runmessagechanged_actionevent (final runningmevent E)
{
System. Out. println ("execution event method! "+ E. getmessage ());
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Useeventlistener useevent = new useeventlistener ();
Useevent. testlistener ("parameters passed by the event ");
}
}

OK. So far, an example of Custom Event listening has been written. (Study hard and make progress every day !)

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.