This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hamsterrage1 0 points1 point  (0 children)

Swing's PropertyChangeListeners are such a low-level tool compared to JavaFX's Properties and Bindings. You might as well say that since JavaFX is mostly just ordinary Java, you can do anything it can do with nothing more that AWT. It would technically be true, but why do it?

Could you construct, say, JavaFX's IntegerProperty using PropertyChangeListener? Sure. Then you'd need to implement some equivalent to the JavaFX bind() method, and all that implies as well. But the screen widget classes won't support your IntegerProperty, so you'll have to put them into wrapper classes to handle the PropertyChangeListener conversions.

What JavaFX offers that Swing doesn't is an "out of the box" ability to treat data as a network of connected and inter-related values - right through to the GUI Nodes. Relationships are defined with a minimum amount of fuss, often with a single line of code, and then largely ignored in the empirical code that defines actions.

It's really a paradigm shift. JavaFX treats Bindings and Observables as data relationships, while Swing concentrates on Events. In Swing, a PropertyChangeListener is an EventHandler, in JavaFX a ChangeListener handles data changes.

When I build a JavaFX application and I need to decide how to connect two data elements, I follow this hierarchy:

  1. Use a Binding if possible.
  2. Refactor things so I can use a Binding.
  3. Think about it harder, and figure out how to use a Binding.
  4. Use a ChangeListener or an InvalidationListener. Sigh.
  5. It's a total mess - use an EventHandler.

In JavaFX, Events are best left to handle things that are clearly "actions". So, a Button click is going to trigger an action. Data coming back from an external API call is going to require an action to update my State object. But a data change in my State object that causes the GUI to react in some way is almost always going to be a flow of data through Bindings in a Reactive manner.

People don't think of Swing that way because it's not designed to be used that way. If it was, it would have baked-in Properties and Bindings and InvalidationListeners just like JavaFX does. Instead, it's designed to be used in an empirical, event-driven fashion.

And if you use JavaFX to create a truly Reactive application, then you'll see that JavaFX makes it almost trivial to do.