更新时,LiveList被调用两次

时间:2017-12-05 10:57:56

标签: java javafx reactfx

我正在使用LiveList将组的子项绑定到包含子项数据的列表。这是一个例子:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        ObservableList<Integer> intList = FXCollections.observableArrayList();
        LiveList<Circle> circleList = LiveList.map(intList, i -> {
            System.out.println("in");
            return new Circle(i);
        });

        Group group = new Group();
        Bindings.bindContent(group.getChildren(), circleList);

        intList.add(2);
        intList.clear();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我的问题是,对于intList中的每个更改,绑定列表会更新两次,并且会创建比所需更多的对象。运行代码给出:

in
in
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = Group@68887c42
    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 org.reactfx.collection.ChangeListenerWrapper.onChange(LiveList.java:439)
    at org.reactfx.collection.ChangeListenerWrapper.onChange(LiveList.java:417)
    at org.reactfx.util.ListNotifications.lambda$takeHead$0(NotificationAccumulator.java:317)
    at org.reactfx.ObservableBase.notifyObservers(ObservableBase.java:68)
    at org.reactfx.ObservableBase.notifyObservers(ObservableBase.java:57)
    at org.reactfx.collection.MappedList.sourceChanged(MappedList.java:41)
    at org.reactfx.collection.LiveList.lambda$observeQuasiChanges$7(LiveList.java:256)
    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 com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:157)
    at test1.Main.start(Main.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)

我希望intList中的每个元素都可以在群组的子项中有Circle。为什么会发生这种情况?如何使其正常工作?

1 个答案:

答案 0 :(得分:-1)

异常描述是一个红色的鲱鱼 - 消息&#34;重复的儿童添加&#34;在更改结束时,子项的数量与变更预测的数量不匹配。 在您的情况下,实际发生的是圈未被移除,因此Group最终只有一个孩子,但clear预测它应该以0结尾。

未删除圆圈的原因是它不是同一个圆圈 - 只要请求执行映射,映射器就会创建一个新的Circle对象,因此在添加时会得到1个圆圈,清除列表时完全不同 - 这个新的Circle无法从组的子项中删除,因此即使在调用clear后触发异常也会保留1个孩子。

要解决此问题,您必须确保为同一源对象返回相同的目标对象 - 如果您的整数列表可能包含重复项,则可能无法实现。

相关问题