Andy
Andy

Reputation: 1423

How to sort node in the VBox in JavaFX? I Have a "duplicate children added" Exception

I have got a JavaFX 8 VBox,and want to sort node in the VBox,But I have a Exception"Children: duplicate children added".

    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);
        }
    }

I found a quick fix,but I think there might be a better solution.

  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);
        }
    }

This is the full stack trace

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)

Upvotes: 3

Views: 1053

Answers (1)

Newbie
Newbie

Reputation: 11

For me it works like this:

    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);

Where the selectedItemsView is a VBox populated with CheckBoxes that are labled with unique numbers.

Upvotes: 1

Related Questions