Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 为什么即使使用已排序的可观察列表,我的组合框也没有排序?_Sorting_Javafx_Arraylist_Combobox - Fatal编程技术网

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

Sorting 为什么即使使用已排序的可观察列表,我的组合框也没有排序?,sorting,javafx,arraylist,combobox,Sorting,Javafx,Arraylist,Combobox,我试图在javafx中的组合框中显示一个已排序的项目列表 在我的控制器中,我的项目列表声明为以下列表: private final ObservableList<Profile> profiles = FXCollections.observableArrayList(); private final SortedList<Profile> sortedProfiles = new SortedList<>(profiles); 然后,我的控制器中有一个方法

我试图在javafx中的组合框中显示一个已排序的项目列表

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

private final ObservableList<Profile> profiles = FXCollections.observableArrayList();
private final SortedList<Profile> sortedProfiles = new SortedList<>(profiles);
然后,我的控制器中有一个方法可以添加项目:

profiles.add(new Profile(profileName));
组合框已更新,但未排序。为什么?我认为使用sortedlist包装器可以使组合框保持排序

示例代码:

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);
        }
    }
}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.collections.transformation.SortedList;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ComboBox;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
导入java.util.Random;
公共类演示扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
public void start(Stage primaryStage)引发异常{
最终ObservableList items=FXCollections.observableArrayList();
增加(新的第(1)项);
增加(新项目(100));
增加(新的第(10)项);
最终分拣清单项目分拣清单=新分拣清单(项目);
最终边框窗格视图=新建边框窗格();
最终组合框配置文件=新组合框();
最终按钮添加=新按钮(“添加随机”);
add.setOnAction(事件->items.add(新项(new Random().nextInt(5000)));
profiles.setItems(itemSortedList);
视图。机顶盒(配置文件);
视图.setBottom(添加);
最终场景=新场景(视图,400400);
初级阶段。场景(场景);
primaryStage.show();
}
私有静态最终类项实现可比较{
私有整数名;
公共项(最终整数名){
this.name=名称;
}
@凌驾
公共字符串toString(){
返回“Int:+名称;
}
@凌驾
公共int比较(最终项目o){
返回name.compareTo(o.name);
}
}
}

您从未设置排序列表的
比较器属性。包含以下关于
比较器
属性的语句:

表示此分类列表顺序的比较器。未排序的分类列表为空

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

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

示例代码添加据我所知,
组合框
已正确排序。请记住,
String
是按字典进行比较的。我已经更改了演示代码,现在使用整数代替字符串,没有任何更改。我甚至在应用程序的开头添加了一些数据,故意不排序。啊,我明白为什么我不能重现这个问题了。我会写一个答案。没关系,费边比我快。对于自然排序,也可以使用。谢谢!这确实是解决办法。现在的问题是,为什么存在允许不设置比较器的构造函数!我的意思是,sortedlist是用来对列表进行排序的,但是我们可以不进行排序就使用它……在某些情况下,您可能需要进行绑定,从而不需要进行初始化。它在fxml中也很有用,因为
null
可能不是在那里生成的最简单的值。。。
final SortedList<Item> itemSortedList = new SortedList<>(items, Comparator.naturalOrder());
final SortedList<Item> itemSortedList = new SortedList<>(items, Comparator.comparing(Item::getName));