Skip to content

Instantly share code, notes, and snippets.

@RupprechJo
Last active December 29, 2015 08:59
//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());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment