When I started using JavaFX we were coming from having worked (for a short while) with Swing. Our approach to JavaFX was to use it just like it was Swing, but with different widgets and method names.

This meant that we wrote code that loaded data into screen nodes, and then we had to scrape it out again in the code that ran when the “Save” Button was clicked. When we had nodes that were dependent on other nodes, then we had handlers that ran when focus was gained or lost, so that we could update those other nodes. Or key listeners that would run when “Enter” was pressed.

Stuff like that.

Eventually we learned about Bindings, and we started to develop “rules” about how we would use Bindings. These were things like: When you had multiple properties in your layout that relied on some element in your Presentation Model, bind them all to that property in the Presentation Model, don’t chain them off each other. For instance, if we had 3 Buttons that needed to be visible together based on some data, don’t bind the first one’s visible property to the data, and the second one’s to the first one’s visible property. Bind them all to the data.

At some point we realized that bidirectional binding of data entry nodes’ value properties to the Presentation Model meant that we didn’t have to scrape it out inside the “Save” Button code. Eventually we realized you don’t even need to send any of that data anywhere from the “Save” Button code because the Presentation Model itself can be shared with the back-end logic - so it already had it.

All of this took years to figure out. Yes, years. And over that time I had the experience of going back to some code that we had written years earlier and rewriting it. Every single time I did this, the new code was better and, without exaggeration, only about 10-20% as big as the original. That’s because doing stuff the “wrong” way, which was the only way we knew how at first, was clumsy and took way more code.

Only later did I learn that the approach that we had evolved (stumbled) into was called a “Reactive UI”. We didn’t try to get there. We just kept refining our approach and finding better techniques as we learned new things, and each incremental improvement moved us closer to Reactive GUI development.

Using JavaFX in a Reactive way, which I believe is the way that it is intended to be used, means that you need to understand the fundamental reactive techniques. This boils down to understanding Bindings, Listeners, Subscriptions and EventHandlers and understanding where you should use each one.

This article is my attempt to explain all that:

https://www.pragmaticcoding.ca/javafx/elements/events_and_listeners

Take a look and let me know what you think.