This discussion is archived
5 Replies Latest reply: Jul 31, 2012 5:40 AM by 885691 RSS

Custom Event

949137 Newbie
Currently Being Moderated
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?

pseudo
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) {
          super(arg0);
          // TODO Auto-generated constructor stub
     }

     public myEvent () {
          this(MY_EVENT);
     }
}

public class myClass implements EventHandler<myEvent> {
     @Override
     public void handle(TimelineDropEvent arg0) {
          // TODO Auto-generated method stub
          System.out.println("MyEvent");
     }
}

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
    885691 Pro
    Currently Being Moderated
    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
    949137 Newbie
    Currently Being Moderated
    An example would be appreciated
  • 3. Re: Custom Event
    885691 Pro
    Currently Being Moderated
    Sorry for the delay. I had to create a Google Code project to host the example:

    http://code.google.com/p/sjmb/

    The source code for the message bus API is here:

    http://code.google.com/p/sjmb/source/browse/#svn%2Ftrunk%2Fsjmb%2Fsrc%2Fcom%2Foracle%2Fsjmb

    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:

    http://code.google.com/p/sjmb/source/browse/#svn%2Ftrunk%2Fsjmb%2Ftest%2Fcom%2Foracle%2Fsjmb%2Ftest

    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:

    http://code.google.com/p/sjmb/downloads/list

    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.

    Greg
  • 4. Re: Custom Event
    949137 Newbie
    Currently Being Moderated
    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
    885691 Pro
    Currently Being Moderated
    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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points