Built in Bindings java
Probably you know the JavaFx property binding. ( If not here is a tutorial ). 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 ... oracle
Here we have certain excamples for that: ide
//Binding with the bind method of a property StringProperty prop1 = new SimpleStringProperty(); StringProperty prop2 = new SimpleStringProperty(); prop1.bind(prop2); prop2.set("Hello World"); Assert.assertEquals(prop1.get(), prop2.get()); //Binding with Bindings.add method IntegerProperty 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());
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:
ui
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. this
Implement the computeValue method, which returns the new value of the binding. spa
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.
code
Here are several examples for that: orm
//Simple custom IntegerBinding implementation IntegerProperty 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 dependencies ObjectProperty<BigDecimal> price = new SimpleObjectProperty<>(); ObjectProperty<BigDecimal> amount = new SimpleObjectProperty<>(); ObjectBinding<BigDecimal> totalPrice = new ObjectBinding<BigDecimal>() { { 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 dependencies ObjectProperty<Bounds> boundsInLocalProperty = new SimpleObjectProperty<>(); DoubleProperty widthProperty = new SimpleDoubleProperty(); DoubleProperty heightProperty = new SimpleDoubleProperty(); ObjectBinding<Rectangle2D> rectBinding = new ObjectBinding<Rectangle2D>() { {bind(boundsInLocalProperty, widthProperty, heightProperty);} protected Rectangle2D computeValue() { Bounds bounds = boundsInLocalProperty.get(); return new Rectangle2D(bounds.getMinX(), bounds.getMinY(), widthProperty.get(), heightProperty.get()); } };