且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建可观察列表/集合

更新时间:2022-03-29 09:35:53

使用 FXCollections

ObservableList<String> list = FXCollections.observableArrayList();

选择框构造函数中的不安全操作是因为您尚未指定选择框的类型:

The unsafe operation in your choice box constructor is because you haven't specified the type for the choice box:

ChoiceBox<String> box = new ChoiceBox<>(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip"));

并且 SortedList 的错误是因为是没有没有参数的构造函数。 (同样,请参阅 javadocs 。)有两个构造函数:最简单的引用一个 ObservableList (排序列表将为其提供排序视图的列表)。所以你需要像

and the error from SortedList is because there is no constructor taking no arguments. (Again, refer to the javadocs.) There are two constructors: the simplest one takes a reference to an ObservableList (the list for which the sorted list will provide a sorted view). So you would need something like

SortedList<String> sortedList = new SortedList<>(list);

SortedList<String> sortedList = new SortedList<>(FXCollections.observableArrayList());