Error when attempting to remove elements from an observable mapped by EasyBind.map()

53 Views Asked by At

The following code gives me trouble when I click on one of the generated checkboxes:

public class Controller implements Initializable {
    ObservableList<String> strings = FXCollections.observableArrayList("a", "b", "c");

    @FXML
    public HBox x;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Bindings.bindContent(x.getChildren(), EasyBind.map(strings, s -> {
            CheckBox c = new CheckBox(s);
            c.setOnAction(event -> {
                strings.remove(c.getText());
                System.out.println("removed");
            });
            return c;
        }));
    }
}

What I'm aiming at is having an observable list of strings, and that when that list is changed, checkboxes in the HBox get created / removed accordingly.

The error I'm getting is:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = HBox[id=x, styleClass=root]
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator$VetoableSubListDecorator.clear(VetoableListDecorator.java:529)
    at com.sun.javafx.binding.ContentBinding$ListContentBinding.onChanged(ContentBinding.java:114)
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
    at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
    at org.fxmisc.easybind.MappedList.sourceChanged(MappedList.java:37)
    at javafx.collections.transformation.TransformationList.lambda$getListener$23(TransformationList.java:106)
    at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
    at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
    at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
    at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
    at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
    at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:183)
    at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:171)
    at sample.Controller.lambda$null$0(Controller.java:28)

It seems that JavaFX / EasyBind have trouble removing the element, but it's not entirely clear why?

EDIT:

Added some print statement when creating the checkboxes:

Creating checkbox a
Creating checkbox b
Creating checkbox c
Creating checkbox a
Creating checkbox b
Creating checkbox c

Something is triggering twice the creation of checkboxes, but it's still not entirely clear to me what.

Thanks

0

There are 0 best solutions below