I recently wrote function like this in my JavaFX application:
public static <T1, T2> void bindAndSet(ReadOnlyProperty<T1> sourceProperty, Property<T2> targetProperty, Function<T1, T2> converter) {
sourceProperty.addListener(new WeakChangeListener<>((obs, oldVal, newVal) -> targetProperty.setValue(converter.apply(newVal))));
targetProperty.setValue(converter.apply(sourceProperty.getValue()));
}
This is unidirectional bind with transformation and setting initial value.
I have small experience in JavaFX but it seems like it should be quite common part of an application that uses binding. Property#bind
seems nearly useless. It does not allow converting types and from checking source code it does not set initial value.
Is there a method (or 2) that provide such functionality? Or maybe I am using JavaFX API in a wrong way?