There are several predefined event classes in javafx. Event.ANY, KeyEvent.KEY_TYPED, MouseEvent.ANY and so on. There is also advanced filtering and handling system for events. And I'd like to reuse it to send some custom signals.
How can I create my custom event type CustomEvent.Any, emit this event programmatically and handle it in a node?
In general:
Some explanations:
If you want to create an event cascade, start with an "All" or "Any" type, that will be the root of all of the EventTypes:
This makes possible creating descendants of this type:
Then write the
MyEvent
class (which extendsEvent
). The EventTypes should be typed to this event class (as is my example).Now use (or in other words: fire) the event:
If you want to catch this event:
Here is a (slightly over-complicated) sample application demonstrating some of the concepts that eckig outlines in his (excellent) answer.
The sample creates a visual field which is a tiled pane of reactor nodes. A custom lightning event is periodically sent to a random node, which will flash yellow when it receives the event. Filters and handlers are added to the parent field and their invocation reported to system.out so that you can see the event bubbling and capturing phases in action.
The code for the LightningEvent itself was mainly copied directly from the standard ActionEvent code in the JavaFX source, your event code could probably be a little simpler.