为什么即使使用排序的可观察列表,我的组合框也没有排序?

Why even when using a sorted observable list, my combobox is not sorted?

我正在尝试在 javafx 的组合框中显示经过排序的项目列表。

在我的控制器中,我的项目列表声明如下:

private final ObservableList<Profile> profiles = FXCollections.observableArrayList();
private final SortedList<Profile> sortedProfiles = new SortedList<>(profiles);

我的组合框是这样初始化的:

profiles.setItems(controller.getSortedProfiles());

然后,我的控制器中有一个方法可以添加项目:

profiles.add(new Profile(profileName));

组合框已更新,但未排序。为什么 ?我认为使用排序列表包装器可以使组合框保持排序?

示例代码:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.Random;

public class Demo extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) throws Exception {
        final ObservableList<Item> items = FXCollections.observableArrayList();

        items.add(new Item(1));
        items.add(new Item(100));
        items.add(new Item(10));

        final SortedList<Item> itemSortedList = new SortedList<>(items);

        final BorderPane view = new BorderPane();

        final ComboBox<Item> profiles = new ComboBox<>();
        final Button add = new Button("add random");
        add.setOnAction(event -> items.add(new Item(new Random().nextInt(5000))));

        profiles.setItems(itemSortedList);

        view.setTop(profiles);
        view.setBottom(add);

        final Scene scene = new Scene(view, 400, 400);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static final class Item implements Comparable<Item> {
        private Integer name;

        public Item(final int name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Int : " + name;
        }

        @Override
        public int compareTo(final Item o) {
            return name.compareTo(o.name);
        }
    }
}

您从未设置排序列表的comparator 属性。 The javadoc 包含以下关于 comparator 属性 的声明:

The comparator that denotes the order of this SortedList. Null for unordered SortedList.

即在不指定比较器的情况下,列表只是保持原始列表的顺序。只需指定比较器即可解决问题:

final SortedList<Item> itemSortedList = new SortedList<>(items, Comparator.naturalOrder());

或者,如果您添加适当的 getter,您可以轻松地创建一个 Comparator 按给定 属性 排序(前提是此 属性 具有可比性):

final SortedList<Item> itemSortedList = new SortedList<>(items, Comparator.comparing(Item::getName));