Bidirectional binding enhancement

Michael Strauß michaelstrau2 at gmail.com
Wed Nov 10 05:45:46 UTC 2021


JavaFX developers routinely use programming patterns like MVC, MVP, or
MVVM to separate views from their associated business logic. Bindings
can be used to connect the values of UI controls (like Label or
TextField) to properties on a business logic class.

A typical (simplified) scenario may look like this:

var valueField = new TextField();
valueField.textProperty().bindBidirectional(businessLogic.valueProperty());

The business logic class may perform data validation or other actions
on the value that was entered in the TextField. However, in many
cases, it is neither necessary nor desirable for the binding to update
the business-layer property on every single change (i.e. every single
character that was typed by a user). For example, if a business rule
verifies that the data entered by a user is formatted in a specific
way, it's usually not a great experience to yield a validation error
before the user has finished typing. Instead, it's often better to
wait until the user has significantly interacted with a UI control
before running business logic.

For this reason, I propose to add a new type of binding to the
javafx.beans.binding.Bindings class:

void bindBidirectional(Property<T> target, Property<T> source,
UpdateSourceTrigger trigger)

UpdateSourceTrigger is an enumeration that allows developers to
specify the condition on which changes of the target property will
update the source property. Its values are:

DEFAULT: Updates the source property on every change (this is the
default behavior of bidirectional bindings).
FOCUS: Updates the source property when the UI control loses input focus.
ACTION: Updates the source property when the UI control loses input
focus or when it receives an ActionEvent (in the case of TextField,
this corresponds to the ENTER key).

Note that this setting only applies to changes of the target property.
When the source property is changed instead, the target property is
always immediately updated.

Any feedback on this proposal is appreciated.


More information about the openjfx-dev mailing list