Skip to content
Advertisement

How to create a method that adds a listener and allows adding JAVA code

I’ve been trying to make an event system for several days now but I can’t find a way to do the following.

I can’t find a way to make the event interactable from a method, I mean this:

JavaScript

The operation is simple, I would like that when the event is fired to the listeners in that method, I could add my code.

I am relatively new to Java and still struggle with this kind of thing.

I currently have these 5 classes:

This interface corresponds to the Listener, the one that will receive the event.

JavaScript

Implementation of the above.

JavaScript

This interface will contain the methods to add listeners and receive the event by adding code (which I intend to look for in the example above)

JavaScript

This class fires events to declared listeners.

JavaScript

This class is the class from which I intend to get information and events.

JavaScript

Advertisement

Answer

I recommend that use template classes for object-specific handling.

ListenerAdapter:

JavaScript

EventListener:

JavaScript

Simple test:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement