5 Replies Latest reply on Jul 31, 2012 12:40 PM by 885691

    Custom Event

      I need to communicate between two different controllers associate with different sections of FXML.

      I create a custom even and implemented an event handler in one of my controllers.

      I can not seem to figure out how to fire the custom event from my other controller. So far the documentation I have found doesn't fill in the blank about firing an event from a controller or even any other class than one that already has .fireEvent as a member.

      Am I using the wrong design pattern here or is there an easy solution to fire an event?

      public class myEvent extends Event {
           private static final long serialVersionUID = 1261846397820142663L;
           public static final EventType<myEvent >MY_EVENT = new EventType<myEvent >(ANY, "MY_EVENT");
           public myEvent (EventType<? extends Event> arg0) {
                // TODO Auto-generated constructor stub
           public myEvent () {
      public class myClass implements EventHandler<myEvent> {
           public void handle(TimelineDropEvent arg0) {
                // TODO Auto-generated method stub
      public class myOtherClass {
      //TODO what do I do here to fire an event
      Obviously both of my sample classes also implement Initializable since they are controllers.

      In my event do I absolutely need serialVersionUID or is there another way to avoid the warning associated with removing it?
        • 1. Re: Custom Event
          Hard-wiring events will work but it creates a tight coupling between controllers. A simple pub/sub "message bus" might be a better option. Unfortunately the platform does not currently include one, but it isn't terribly difficult to write. I can post an example if you would like. Let me know.
          • 2. Re: Custom Event
            An example would be appreciated
            • 3. Re: Custom Event
              Sorry for the delay. I had to create a Google Code project to host the example:


              The source code for the message bus API is here:


              It essentially allows callers to subscribe to "message topics", which are simply Java types. Any time an instance of a given type is sent, subscribed listeners are notified. Any Java type can be used including enums, which provide a nice, type-safe way to define messages.

              An example demonstrating how to use the API is here:


              The main application window presents a tab pane containing 3 identical tabs. Each tab's controller subscribes to a message of type com.oracle.sjmb.test.Message. Entering text in the text field and pressing the "Send Message" button sends a new message, causing the other two tabs to update the contents of their own text fields to match.

              The example application consists of the following files:

              Message.java - the message class
              MessageBusTest.java - the main JavaFX application class
              message_bus_test.fxml - FXML document containing the main scene
              tab.fxml - FXML document containing tab pane content
              TabController.java - tab content controller

              Javadoc and binaries are available here:


              Please note that this is an example only and is not an official part of any Oracle product. Let me know if you have any questions.

              • 4. Re: Custom Event
                Thanks. I like this concept. It seems that I need to adjust my way of thinking and ignore what I have learned about how c# handles 'events'
                • 5. Re: Custom Event
                  It seems that I need to adjust my way of thinking and ignore what I have learned about how c# handles 'events'
                  Not necessarily. JavaFX also has a concept of "events", which extend the javafx.event.Event class. However, registering for these events requires that the listener be aware of the event source, which is not always practical. A message bus just allows application components (such as view controllers) to communicate with each other in a more decoupled fashion.

                  [edit] Theoretically, an Event subclass could be used as the message type, though I have not tried this.

                  Edited by: Greg Brown on Jul 31, 2012 5:39 AM