gpt4 book ai didi

java - 如何在 JavaFX 中对 VBox 中的节点进行排序?我有一个 "duplicate children added"异常

转载 作者:行者123 更新时间:2023-12-05 06:23:16 26 4
gpt4 key购买 nike

我有一个 JavaFX 8 VBox,想对 VBox 中的节点进行排序,但我有一个异常“ child :添加了重复的 child ”。

    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)

最佳答案

对我来说,它是这样工作的:

    private static class CoparaterAscending<T> implements Comparator<T> {

@Override
public int compare(T o1, T o2) {

int numberNode1 = Integer.parseInt(((CheckBox)o1).getText());
int numberNode2 = Integer.parseInt(((CheckBox)o2).getText());
System.out.println(numberNode1 + " " + numberNode2);
if ((numberNode1 > numberNode2)) {
return 1;
} else if ((numberNode1 < numberNode2)) {
return -1;
}
return 0;
}
}

comp = new CoparaterAscending<Node>();
FXCollections.sort(selectedItemsView.getChildren(), comp);

其中 selectedItemsView 是一个 VBox,其中填充了标有唯一编号的 CheckBox。

关于java - 如何在 JavaFX 中对 VBox 中的节点进行排序?我有一个 "duplicate children added"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58585654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com