JEP Proposal: Fluent Bindings for multiple observables

Andy Goryachev andy.goryachev at oracle.com
Mon Oct 27 19:48:43 UTC 2025


Doesn't

DoubleBinding createDoubleBinding(final Callable<Double> func, final Observable... dependencies)

handles the use case you describe?

-andy

From: openjfx-dev <openjfx-dev-retn at openjdk.org> on behalf of John Hendrikx <john.hendrikx at gmail.com>
Date: Sunday, October 26, 2025 at 02:59
To: openjfx-dev <openjfx-dev at openjdk.org>
Subject: JEP Proposal: Fluent Bindings for multiple observables

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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20251027/10737163/attachment.htm>


More information about the openjfx-dev mailing list