|
Usage
# events-on-fire usage UsageInclude the libraryInclude events-on-fire manuallyDownload the latest release:
Add the jar manually to your build and execution path. Include events-on-fire via MavenAdd the following dependency: <dependency> <groupId>com.google.code.events-on-fire</groupId> <artifactId>events-on-fire</artifactId> <version>1.2.2</version> </dependency> Basic exampleYou can use any object as event. Usually you create a class that holds all the stuff you need for handling the event, but it may be enough to use a String. Create a consumer (your listener)Add a method for the event handler to your class. Tag it with the @EventHandler annotation: public class MyClass {
...
@EventHandler
public void onEvent(String event) {
// handle the event
...
}
...
}There are more variations of signatures of the event handler method. Check EventHandlerVariations for more... Bind the consumer to a producerEvents.bind(producer, consumer); The producer usually is your controller. In Swing this may be your Frame or a Component. In Web-Applications this may be the instance of a service. The consumer is the class with the event handler. Keep in mind, that you have to handle the instance of the consumer. The references in the Events class are weak. If there is no reference left, the consumer will get cleaned up by the garbage collector. Fire the eventEvents.fire(producer, "some event"); Immediately all the event handlers of the consumers, which are bonded to the producer will get called. The consumer will only get called, if it has a event handler method that fits the event. |