2

Given two properties such as

DoubleProperty x;
DoubleProperty y;

and given two functions that are inverses of each other, such as

DoubleUnaryOperator xToY = x -> Math.exp(x);
DoubleUnaryOperator yToX = y -> Math.log(y);

what is the simplest way to bidirectionally bind properties x and y such that they retain the relation defined?

(i.e. If we assign to x then y should automatically become xToY.apply(x.get()) and if we assign to y then x should automatically become yToX.apply(y.get()).)

Is there a way in the bindings API to do this?

Museful
  • 6,711
  • 5
  • 42
  • 68
  • 2
    There's nothing in the API that does this; you would have to implement it with two listeners (being careful to avoid stack overflow exceptions which could be caused by rounding errors, as `yToX.apply(xToY.apply(x.get()))==x.get()` is not guaranteed with double arithmetic). Note though that Tomas Mikula's ReactFX library has this functionality built into the `Var` API. See his [blog](http://tomasmikula.github.io/blog/2015/02/10/val-a-better-observablevalue.html) – James_D Jan 13 '16 at 15:04
  • 1
    You may want to take a look at [this question](http://stackoverflow.com/questions/20703544/bidirectional-binding-with-objectbinding-in-javafx). – Nikos Paraskevopoulos Jan 13 '16 at 16:55
  • Does the solution i gave you [here](http://stackoverflow.com/questions/34439109/bidirectionally-bind-to-property-negation/34641134#34641134) work for this? – Inge Jan 13 '16 at 18:08
  • @Inge Of course listeners would "work", but the whole point of bindings is to abstract us away from that level. BTW that code may need an extra if condition to avert infinite recursion. Then it would be basically the same as JavaFX's `BidirectionalBinding`, which is what I ended up customizing. – Museful Jan 13 '16 at 19:08

0 Answers0