BindingUtils
Synchronizes a ToggleGroup's selection with an
ObjectProperty: each toggle carries a domain value in its
user data, and selecting a toggle writes that value into the
property. Ideal for radio-button groups backing an enum or model field.
Sincroniza la selección de un ToggleGroup con un
ObjectProperty: cada toggle lleva un valor de dominio en su
user data, y seleccionar un toggle escribe ese valor en la propiedad.
Ideal para grupos de radio buttons que respaldan un enum o campo del modelo.
How it worksCómo funciona
Every toggle's userData holds the value it represents. On
bind, the manager selects the toggle whose user data equals the property's
current value, then installs a listener that pushes the selected toggle's
user data into the property on every change. The listener is stashed in the
group's property map so it can be removed later by
unbindToggleGroupToProperty.
El userData de cada toggle guarda el valor que representa. Al hacer
el bind, el manager selecciona el toggle cuyo user data es igual al valor
actual de la propiedad, y luego instala un listener que empuja el user data del
toggle seleccionado a la propiedad en cada cambio. El listener se guarda en el
mapa de propiedades del grupo para poder eliminarlo después con
unbindToggleGroupToProperty.
Quick startInicio rápido
ToggleGroup roleGroup = new ToggleGroup();
adminRadio.setToggleGroup(roleGroup); adminRadio.setUserData(Role.ADMIN);
userRadio.setToggleGroup(roleGroup); userRadio.setUserData(Role.USER);
ObjectProperty<Role> selectedRole = new SimpleObjectProperty<>(Role.USER);
// Two-way: picking a radio updates the property; the matching radio is
// pre-selected from the property's initial value.
BindingUtils.bindToggleGroupToProperty(roleGroup, selectedRole);
// Later, when tearing the screen down:
BindingUtils.unbindToggleGroupToProperty(roleGroup, selectedRole);
API referenceReferencia de API
Creates the two-way binding. Pre-selects the toggle matching the property's current value, then keeps the property in sync with selection.Crea el binding bidireccional. Preselecciona el toggle que coincide con el valor actual de la propiedad y luego mantiene la propiedad sincronizada con la selección.
Throws NullPointerException if either argument is null, and IllegalArgumentException if any toggle in the group has null user data.Lanza NullPointerException si cualquiera de los argumentos es null, y IllegalArgumentException si algún toggle del grupo tiene user data null.
Removes the previously-installed listener from the group. Both arguments are validated as non-null (property is kept for API symmetry).Elimina del grupo el listener instalado previamente. Ambos argumentos se validan como no null (property se mantiene por simetría de la API).
If even one toggle lacks user data, bind throws immediately — set setUserData(...) on all of them first. The user-data type must match the property's type parameter T.Si aunque sea un toggle carece de user data, bind lanza excepción de inmediato — fija setUserData(...) en todos ellos primero. El tipo del user data debe coincidir con el parámetro de tipo T de la propiedad.
The listener pushes toggle selection into the property, and the initial sync pulls the property's starting value into the selection. Programmatic property changes after binding won't move the selection — drive the UI through the toggles, or re-select manually.El listener empuja la selección del toggle hacia la propiedad, y la sincronización inicial lleva el valor de partida de la propiedad a la selección. Los cambios programáticos de la propiedad después del bind no moverán la selección — controla la UI a través de los toggles, o reselecciona manualmente.
RecommendationsRecomendaciones
Do always pair a bind with an unbind when the view is disposed, to avoid leaking the listener on a long-lived ToggleGroup.Empareja siempre un bind con un unbind cuando la vista se descarte, para no fugar el listener en un ToggleGroup de larga vida.
Do use enum constants as user data for clean, type-safe radio groups.Usa constantes de enum como user data para grupos de radio limpios y con tipos seguros.