Java event processing

Source: Internet
Author: User

In JAVA programming, event processing is very important, especially when you need to customize events and design JavaBean. it is helpful for programming to have a complete understanding of the event processing process.
The following uses a demo to describe the event and its handling process.

I. Composition of events
To customize an event, you must provide an event listening interface and an event class. The listening interface in JAVA inherits java. util. eventListener, the event class inherits java. util. eventObject. many basic events have been provided in the programming environment for ease of use, but you must understand these in custom events.

The following is the code of an event class. The event class can provide the information of the monitored class to the user's handler.
Import java. util .*;
Public class PropertyEvent extends EventObject {
Public PropertyEvent (){}
}
The following is the code of the listener interface.
Import java. util .*;

Public interface PropertyListener extends EventListener {
Public void propertyChanged (PropertyEvent propertyEvent );
}

Ii. event handling mechanism

The following is a brief piece of listened-on code. It analyzes the event processing process through code.
Import java. util .*;

Public class Exam {
Private int property;
// Listeners is used to store registered listening objects.
Private Set listeners = new HashSet ();
.....
Public void addListener (PropertyListener propertyListener ){
// Ensure that only one thread can access listeners.
Synchronized (listeners ){
Listeners. add (propertyListener );
}
}
Public void firePropertyChange (){
Iterator iterator;
Synchronized (listeners ){
// Place the class name in listeners to iterator
Iterator = new HashSet (listeners). iterator ();
}

// Create an event class
PropertyEvent propertyEvent = new PropertyEvent ();
While (iterator. hasNext ()){
PropertyListener propertyListener = (propertyListener) iterator. next ();
// Call the user's event handler
PropertyListener. propertyChanged (propertyEvent );
}
}
}
When the attribute value changes, the firePropertyChange method is called for internal processing to generate an event object, and then the event object is used as a parameter to call the user's event processing program.


Iii. Use of Event Processing
1. Basic usage
Public Exam exam;
Exam. addListener (this );
Public void propertyChange (PropertyEvent event ){...}

Note: exam is the Monitored object, this is the listening object, is the current class that has implemented the interface method, addListener
Register the current class to listeners.

2. A Monitored object can have multiple listening objects

Exam. addListener (listener1 );
Exam. addListener (listener2 );
In this way, when the property of exam changes, the processing programs of actionListener1 and actionListener2 will
Called. Of course, both listener1 and listener2 must be classes that have implemented interface methods.

3. the Monitored object can also be an interface that implements the method.
Exam. addListener (
New PropertyListener (){
// User-defined event handling process
Public void propertyChange (PropertyEvent event ){
...
}
);
This method is very convenient in actual programming.

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.