This is an incredibly barebones example of how Data Binding in JavaFX2 can simplify MVC organized application development. It does so in the context of a well organized project with clearly defined MVC components.
This example should clearly demonstrate why you want to jump right in to fully embracing the data-binding capabilities
of JavaFX2 rather than writing your own ChangeListeners and onChange() methods and wiring up
everything yourself. In fact, the code really is contained in once very simple line:
guiWidget.valueProperty().bindBidirectional(inMemoryObj.property());Now changes in the underlying data represented in the GUI Widget will be automatically reflected in the in-memory data model, and vice-versa. One line of code, that's it.
no Listeners, no Event handlers, no extra wiring = awesome (IMO)...
Take a look! It is a really simple project but that is the point, right?
If you'd like to demo this project without building it, just run the jarfile:
> java -jar packages/binding-ex.jar
This should run on >=Java7u6.
NOTE: I included my intelliJ-14 project files if you'd like to open it in that IDE. If anyone asks I'll output the Eclipse project files too.
NOTE2:
BTW, my motivation for sharing this was partly due to my own current research (Jan 2015) into using JavaFX resulting in
the realization that a lot of tutorials, code examples, and StackOverflow answers all suggest that the best practice
when handling data binding in JavaFX still requires your own Listeners and wiring. False! If you have any problems
transforming one object to another so that you can use the .bindBidirectional() methods, most likely you
need to review the Bindings class for the appropriate converter / wiring. In my example, I do wire an
Integer model property to a GUI text label without any custom Listeners, for example:
Bindings.bindBidirectional(intLabel.textProperty(), inMemoryObj.prop1Property(), new NumberStringConverter());