JEP Proposal: Fluent Bindings for multiple observables
John Hendrikx
john.hendrikx at gmail.com
Sun Oct 26 09:59:37 UTC 2025
JEP: https://gist.github.com/hjohn/611acb65769b68a845b8919c62a3e99a
Hi everyone,
I'd like to propose an extension to the fluent bindings API on
ObservableValue (map, flatMap, orElse) which were introduced in JavaFX
19 over 3 years ago.
The API currently is very powerful when dealing with a single
observable, but lacks support when dealing with multiple observables.
For example, let's say you want to compute a width/height ratio. You
could write this:
ObservableValue<Double> ratio = width.map(w -> w / height.get());
... but you'll quickly find that such an observable will not update
itself when height changes, only when width changes.
The go-to solution for this is currently:
DoubleBinding ratio = new DoubleBinding() {
{ bind(width, height); }
protected double computeValue() { return width.get() /
height.get(); }
}
My proposal would extend ObservableValue with a new `with` method that
returns an intermediate stage that can be easily converted back to an
ObservableValue:
ObservableValue<Double> ratio = width.with(height).map((w, h) -> w /
h); // yields a ratio that updates whenever w or h changes
Or for example:
ObservableValue<Point> point = x.with(y).map(Point::new); //
yields a Point that updates whenever x or y changes
The intermediate stage would not be an observable value itself. This
limits the API surface, and makes this proposal fairly lightweight and
much easier to implement.
Please see the JEP for the full proposal. I look forward to your feedback!
--John
More information about the openjfx-dev
mailing list