This content has been marked as final. Show 5 replies
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.
An example would be appreciated
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.
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'
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.
 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