AlwaysNeedingHelp
AlwaysNeedingHelp

Reputation: 1955

How to bind two SimpleDoubleProperty's with a mathematical operation?

I can bind a SimpleDoubleProperty to some text easy enough:

getSomeTextProperty().bind(Bindings.format("%,.0f", getSomeDoubleProperty()));

Is it possible to do something to the effect of:

getSomeTextProperty().bind(Bindings.format("%,.0f", getSomeDoubleProperty()+getAnotherDoubleProperty()));

The idea being whenever someDoubleProperty or anotherDoubleProperty changes, the text updates with the addition of both of them (or subtraction, or multiplication etc).

Upvotes: 1

Views: 357

Answers (1)

fabian
fabian

Reputation: 82491

The StringExpression created by Bindings.format is updated when one of the arguments is an ObservableValue that changes. DoubleProperty.add creates such an object.

DoubleProperty v1 = new SimpleDoubleProperty();
DoubleProperty v2 = new SimpleDoubleProperty();
StringExpression sb = Bindings.format("%,.3f", v1.add(v2));
System.out.println(sb.get());
sb.addListener((a, b, newValue) -> System.out.println(newValue));
v1.set(3);
v2.set(5.5);

Upvotes: 4

Related Questions