Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java event mechanism

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the Java event mechanism". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the Java event mechanism".

Delegated event model

Java has defined a standard and consistent way of obtaining and handling events based on the delegated event model since 1.1. Its idea is very simple: the event source initiates a specific event and sends the event to one or more event listeners, while the listener waits until it is received, then processes the event and returns. It is also easy to implement:

Define event

Implement a specific listener interface to receive specific types of events

Implement the code to register (or unregister) the listener as the recipient of a specific event type

Trigger an event at the right time

Core component

In this event handling mechanism of Java, there are three core components:

An event object that describes a change in phase. For example, the click of an action in GUI, the start and stop of containers in Spring Framework, more such as computer boot, shutdown, hibernation, cache expiration, official account attention, customs collection, and so on.

The event source can be any object, which has the ability to trigger the event. Listeners are typically registered (or deactivated) in this object, and event triggers are usually here. A source may produce multiple different types of events, registering event listeners for different event types, and each event type can register one or more listeners.

An event listener is a class that implements a specific interface, needs to implement specific handling methods for a particular event, and must be registered with that particular event.

So the question is, how do we elegantly trigger and handle a custom event?

Custom event

Customizing events in Java is very simple. Considering that there is a need to bind social accounts in various applications, let's take this as an example to simply print a record when a social account is bound or unbound.

First, define an event object with the following code:

Public class SocialEvent extends EventObject {private static final long serialVersionUID =-5473622737706032666L; public static final int WECHAT_BIND = 1; public static final int WECHAT_UNBIND = 2; public static final int WEIBO_BIND = 3; public static final int WEIBO_UNBIND = 4; private int socialType; public SocialEvent (Object source, int socialType) {super (source); this.socialType = socialType;} public int getSocialType () {return socialType } public void setSocialType (int socialType) {this.socialType = socialType;}}

The event class must be a subclass of EventObject. It is worth mentioning that event objects usually represent a class rather than an event, that is, it is reasonable to integrate a class of events rather than an event concept.

Next, we implement a set of event handling logic, that is, event listeners:

Public class SocialEventListener implements EventListener {public void onSocialChanged (SocialEvent event) {switch (event.getSocialType ()) {case SocialEvent.WECHAT_BIND: System.out.println ("Wechat bind."); break; case SocialEvent.WECHAT_UNBIND: System.out.println ("Wechat unbind."); break Case SocialEvent.WEIBO_BIND: System.out.println ("Weibo bind."); break; case SocialEvent.WEIBO_UNBIND: System.out.println ("Weibo unbind."); break; default: System.out.println ("Bad social type."); break }}}

In addition, we need an event source:

Public class Social {private List listeners; public void addListener (SocialEventListener listener) {if (listeners = = null) {listeners = new ArrayList ();} listeners.add (listener);} public void removeListener (SocialEventListener listener) {if (listeners! = null) {listeners.remove (listener) }} public void emitEvent (SocialEvent event) {for (SocialEventListener listener: listeners) {listener.onSocialChanged (event);}} public List getListeners () {return listeners;} public void setListeners (List listeners) {this.listeners = listeners;}}

Here, we define a special class Social as the event source. In fact, you can implement the event trigger and registration logic in any other class, such as the startup class.

By the way, in Java GUI programming, we usually only need to register event listeners for components, regardless of the event trigger logic, because their events are automatically triggered by the system.

Thank you for your reading. the above is the content of "what is the Java event mechanism". After the study of this article, I believe you have a deeper understanding of what is the Java event mechanism, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report