当我单击生成的复选框之一时,以下代码给我带来了麻烦:
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;
}));
}
}
我的目标是拥有一个可观察的字符串列表,并且当该列表发生更改时,HBox 中的复选框会相应地创建/删除。
我得到的错误是:
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)
似乎 JavaFX / EasyBind 在删除元素时遇到了麻烦,但不完全清楚为什么?
编辑:
创建复选框时添加了一些打印语句:
Creating checkbox a
Creating checkbox b
Creating checkbox c
Creating checkbox a
Creating checkbox b
Creating checkbox c
某些东西触发了两次复选框的创建,但我仍然不完全清楚是什么。
谢谢