如何在JavaFX中对VBox中的节点进行排序?我有一个;添加重复的子项";例外情况

如何在JavaFX中对VBox中的节点进行排序?我有一个;添加重复的子项";例外情况,java,javafx,Java,Javafx,我有一个JavaFX8VBox,希望在VBox中对节点进行排序,但我有一个异常“子节点:添加了重复的子节点” 发生此异常可能是因为列表的sort方法在排序时再次将元素添加到列表中。因此,你的解决方案是使用列表的副本进行排序,我认为这是一个非常好的解决方案。如果我不得不猜测,这可能需要使用ListView而不是VBox来完成。我非常确定Tobias是正确的,您的解决方案似乎是实现这一点的唯一方法。用于子类列表的可观察列表是VetoableListDecorator的一个实例,而decorator没

我有一个JavaFX8VBox,希望在VBox中对节点进行排序,但我有一个异常“子节点:添加了重复的子节点”


发生此异常可能是因为列表的
sort
方法在排序时再次将元素添加到列表中。因此,你的解决方案是使用列表的副本进行排序,我认为这是一个非常好的解决方案。如果我不得不猜测,这可能需要使用
ListView
而不是
VBox
来完成。我非常确定Tobias是正确的,您的解决方案似乎是实现这一点的唯一方法。用于
子类
列表的
可观察列表
VetoableListDecorator
的一个实例,而decorator没有覆盖
列表#排序
。这会导致一个问题,因为默认排序方法本身调用
set
,而不首先删除任何元素,因此
IllegalArgumentException
。我认为这是一个错误,因为另一个代码>观察者> /COD>实现重写<代码>排序< /代码>,以便在整个操作中只触发一个更改事件。您的解决方案可能是唯一的解决方法。
    private void addTitledPane(ObservableList<TomatoTask> addList) {
        if (!addList.isEmpty()) {

            TitledPane titledPane = new TitledPane(addList.get(0).getDate());
            titledPane.setItems(addList);

            stackedTitledPanes.getChildren().add(titledPane);
            stackedTitledPanes.getChildren().sort(comparatorTitledPane);
        }
    }
  private void addTitledPane(ObservableList<TomatoTask> addList) {
        if (!addList.isEmpty()) {

            TitledPane titledPane = new TitledPane(addList.get(0).getDate());
            titledPane.setItems(addList);

            stackedTitledPanes.getChildren().add(titledPane);

            List list = new ArrayList(stackedTitledPanes.getChildren());

            list.sort(comparatorTitledPane);
            Collections.reverse(list);
            stackedTitledPanes.getChildren().clear();
            stackedTitledPanes.getChildren().addAll(list);
        }
    }
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@2cdae672
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator$VetoableListIteratorDecorator.set(VetoableListDecorator.java:768)
    at java.util.List.sort(List.java:482)
    at app.control.mytomato.StackedPanes.addTitledPane(StackedPanes.java:93)
    at app.control.mytomato.StackedPanes.access$000(StackedPanes.java:16)
    at app.control.mytomato.StackedPanes$3.onChanged(StackedPanes.java:62)
    at com.sun.javafx.collections.MapListenerHelper$SingleChange.fireValueChangedEvent(MapListenerHelper.java:163)
    at com.sun.javafx.collections.MapListenerHelper.fireValueChangedEvent(MapListenerHelper.java:72)
    at com.sun.javafx.collections.ObservableMapWrapper.callObservers(ObservableMapWrapper.java:115)
    at com.sun.javafx.collections.ObservableMapWrapper.put(ObservableMapWrapper.java:173)
    at app.control.mytomato.StackedPanes.addItem(StackedPanes.java:127)
    at app.control.mytomato.StackedPanes.addItems(StackedPanes.java:119)
    at app.view.EditDialogControl.lambda$handleOkButton$0(EditDialogControl.java:88)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$3(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)