Simple custom event management in Java

In Java, every time I want to create a new custom event, I usually do this by adding 3 methods, namely:

addDogEventListener(EventListener listener); removeDogEventListener(EventListener listener); dispatchDogEventListener(DogEvent event); 

Then, if I want to send another event, say CatEvent, I will have to create all these 3 methods again:

 addCatEventListener(EventListener listener); removeCatEventListener(EventListener listener); dispatchCatEventListener(CatEvent event); 

Then, if I want to control only one kind of CatEvent event, say Meow, do I need to copy all these 3 methods again and again ?! How do addCatMeowEventListener (); ... etc?

And, as a rule, I need to send more than one kind of event. It will be very untidy for the whole class to be populated with many methods for passing and handling events. In addition, these functions have very similar code, for example, looping through EventListenerList, adding events to the list, etc.

Is this how I should do event dispatching in Java?

Is there any way I can do this:

 mainApp.addEventListener(CatEvent.MEOW, new EventHandler() { meowHandler(Event e) { }); mainApp.addEventListener(CatEvent.EAT, new EventHandler() { eatHandler(Event e) { }); myCat.addEventListener(DogEvent.BARK, new EventHandler() { barkHandler(Event e) { myCat.run() }); 

That way, I can just handle different CatEvent types in different classes and eventHandler functions, and I don’t need to continue to create different methods of listening for events for different events?

Maybe something is missing for me in handling Java events, but is there a tidier way that I do not need to save and paste into 3 methods, and also create so many different event objects for all the different methods that I want to send?

Thanks!

+4
source share
2 answers

Then, if I want to control only one kind of CatEvent event, say Meow, (and EAT)

The "action" of an event (MEOW or EAT) must be data defined in CatEvent. Then your event listener code will check the type of action and perform the appropriate processing.

Perhaps look at TableModelEvent to find out how to do this. It handles insert, delete, and update events using the same event.

Alternatively, you could probably create a common event receiver based on the PropertyChangeListener. The PropertyChangeListener property is used to handle events when various properties of the Swing component change. For example, when you call setForeground () or setBackground () or setFont () or setText () or setIcon. The PropertyChangeListener property uses the getName () method to determine which property has been changed. Thus, for the above methods, the names will be "in the foreground", "background", "font", "text", "icon". See How to use property change listeners for an example of how this might work.

In your case, the names will be "cat" and "dog". This approach will only work if the GeneralEvent you created may contain information related to each of your events (ie, Meow and bark).

0
source

The event handling strategy that I have is to publish by type that may suit you.

I have a broker who can study the listener for annotation, which marks the method as listening for events. Using this approach, you only need to add methods when you want to process a specific class of events.

 interface Subscriber { // marker interface for OSGi } @interface SubscriberCallback { // marker annotation } class Broker { // uses reflections to find methods marked with @SubscriberCallback public void addSubscriber(Subscriber subscriber); public void removeSubscriber(Subscriber subscriber); public <T> void publish(T... events); } class MyListener implements Subscriber { @SubscriberCallback public void onDogEvent(DogEvent... dogEvents) { // called for one or more dog events } @SubscriberCallback public void onCatEvent(CatEvent catEvent) { // called for each CatEvent including subsclass of CatEvent. } } 
+4
source

All Articles