Built in Bindings
Probably you know the JavaFx property binding. ( ). There are several ways to create a binding. The easiest way is to use the bind or bindBidirectional methods of the property you want to bind. For more complex bindings you can use the Bindings class. It provide a lot of special bindings as static methods e.g. ones that calulate number values, concat string properties, bind ObservableLists, ObservableMaps etc., but nothing for BigDecimal ...
Here we have certain excamples for that:
//Binding with the bind method of a propertyStringProperty prop1 = new SimpleStringProperty();StringProperty prop2 = new SimpleStringProperty();prop1.bind(prop2); prop2.set("Hello World");Assert.assertEquals(prop1.get(), prop2.get()); //Binding with Bindings.add methodIntegerProperty int1 = new SimpleIntegerProperty();IntegerProperty int2 = new SimpleIntegerProperty();NumberBinding sum = Bindings.add(int1, int2); int1.set(2);int2.set(3);Assert.assertEquals(5, sum.getValue().intValue());
Custom implemented Bindings
If the built in bindings do not fit your needs, JavaFx provides base classes, that you can use for custom implemented bindings. You find them in the package javafx.beans.bindings and all their names end with Binding .
There are only two things to do in your subclass of one of the Binding classes:-
Think about on which other properties your new binding is dependand of. Normally this are the properties you calculate your new value of. Use the bind method to add those properties to the dependencies of the new binding in the initializer block (as done in the examples below) or in the constructor. If the value of one of those changes, the binding value is newly calculated.
-
Implement the computeValue method, which returns the new value of the binding.
Normally you think of numbers to compute a value. But you can do this with every object, that can be created from other values, if you use the ObjectBinding class.
Here are several examples for that:
//Simple custom IntegerBinding implementationIntegerProperty intVal = new SimpleIntegerProperty();IntegerBinding modulo2 = new IntegerBinding() { { bind(intVal);} protected int computeValue() { return intVal.get() % 2; }};intVal.set(3);Assert.assertEquals(1, modulo2.get());intVal.set(4);Assert.assertEquals(0, modulo2.get()); //Custom ObjectBinding of BigDecimal with two dependenciesObjectPropertyprice = new SimpleObjectProperty<>();ObjectProperty amount = new SimpleObjectProperty<>();ObjectBinding totalPrice = new ObjectBinding () { { bind(price,amount);} protected BigDecimal computeValue() { if (price.get() == null || amount.get() == null) return null; return price.get().multiply(amount.get()); }}; price.set(new BigDecimal("3.50"));amount.set(new BigDecimal("2.0"));Assert.assertEquals(new BigDecimal("7.000"), totalPrice.get()); //Custom ObjectBinding of Rectangle2D with three dependenciesObjectProperty boundsInLocalProperty = new SimpleObjectProperty<>();DoubleProperty widthProperty = new SimpleDoubleProperty();DoubleProperty heightProperty = new SimpleDoubleProperty(); ObjectBinding rectBinding = new ObjectBinding () { {bind(boundsInLocalProperty, widthProperty, heightProperty);} protected Rectangle2D computeValue() { Bounds bounds = boundsInLocalProperty.get(); return new Rectangle2D(bounds.getMinX(), bounds.getMinY(), widthProperty.get(), heightProperty.get()); }};