losing the validation listener
Tom Eugelink
tbee at tbee.org
Mon Apr 8 03:48:45 PDT 2013
Hi Werner,
It indeed is very similar (my code is public on Github, so no use adding it here), especially the selectedToggleProperty listener. I chose to reuse as much of the existing approach, being the getUserData().
What would be of interest to me is:
- the exact declaration of the enumValueProperty
- how you listen to changes on enumValueProperty
- and of course: what happens if you hide/disable the toggles with only one listener attached
Tom
On 2013-04-08 11:51, Werner Lehmann wrote:
> Hi Tom,
>
> I did something similar: toggle group for toggles which correspond to enum members. This one assume that the toggles correspond to the enum members in their declared order. It also uses an invalidation listener and disabling/enabling a toggle keeps the listener functional as I just tested with a test application.
>
> public class MintEnumToggleGroup<E extends Enum<E>> extends ToggleGroup
> {
> public MintEnumToggleGroup(Class<E> enumClass)
> {
> this.enumClass = enumClass;
>
> selectedToggleProperty().addListener(new InvalidationListener()
> {
> @Override
> public void invalidated(Observable observable)
> {
> Toggle toggle = getSelectedToggle();
> E value = null;
> if (toggle != null)
> {
> int ordinal =
> MintEnumToggleGroup.this.getToggles().indexOf(toggle);
> value = MintEnumToggleGroup.this.enumClass
> .getEnumConstants()[ordinal];
> }
> if (enumValue.get() != value)
> enumValue.set(value);
> }
> });
>
> ...
> }
>
> /**
> * Bidirectionally bindable property representing the enum member
> * of the selected toggle.
> */
> public ObjectProperty<E> enumValueProperty() { return enumValue; }
> public E getEnumValue() { return enumValueProperty().get(); }
> public void setEnumValue(E value) { enumValueProperty().set(value); }
> }
>
>
> Looks similar to what you are doing. Let me know if you want to look at the full source (toggle group and testcase).
>
> Rgds
> Werner
>
> On 07.04.2013 21:28, Tom Eugelink wrote:
>>
>> Again some strange behavior I could use some pointers with. In JFXtras I've created an extended ToggleGroup which has a value property.
>> https://github.com/JFXtras/jfxtras-labs/blob/2.2/src/main/java/jfxtras/labs/scene/control/ToggleGroupValue.java
>>
>> Basically what it does is listen to the selectedToggleProperty of ToggleGroup, and upon invalidation gets the user data associated with the now active toggle and puts that in the valueProperty. Simple, and now you can register and listen to the value of the toggle group. Which is exactly what I do in my basketball application by registering to the invalidated event.
>> toggleGroup.valueProperty().addListener(new InvalidationListener() {...});
>>
>> Now I have one very strange behavior; if I disable or hide the toggles, and then re-enable/show them again, the invalidation listener is no longer called. Some how it seems to have been removed from the listeners list. But the API documentation explicitly says it is a strong reference.
>> http://docs.oracle.com/javafx/2/api/javafx/beans/Observable.html#addListener(javafx.beans.InvalidationListener)
>>
>> If I add a second dummy listener, then the first listener is not removed when disabled/hidden.
>>
>> It very much reeks like a garbage collection thing. Any suggestions?
>>
>> Tom
More information about the openjfx-dev
mailing list