I don't think there is a way to do this directly. While a general-purpose
Bindings.bindBidirectional(Property<S> property1, Property<T> property2, Function<S,T> mapping, Function<T,S> inverseMapping)
might make a good addition to the API, even that wouldn't help in this case as the ToggleGroup
's selectedProperty
is read only (since selection needs to be handled when each Toggle
's setSelected(...)
method is invoked, as well as by the ToggleGroup
's selectedProperty
).
Using a couple of listeners is the way to go in this case.
The closest thing to the "custom bidirectional map" is the
Bindings.bindBiDirectional(StringProperty stringProperty, ObjectProperty<T> otherProperty, StringConverter<T> converter)
method. In the case where you have an (writeable) ObjectProperty<S>
and (writeable) ObjectProperty<T>
you can in theory use two bidirectional bindings and an intermediate StringProperty
to bind them together. In practice, this is almost always more code than just using two listeners, and is also less efficient.