创建可观察列表/集合 [英] Creating an Observable List/Collection

查看:105
本文介绍了创建可观察列表/集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaFX 8中创建一个ChoiceBox,它需要一个集合。我不知道如何创建一个集合,如果我尝试:

I'm trying to create a ChoiceBox in JavaFX 8, which requires a collection. I can't figure out how to create a collection though... If I try:

 ObservableList<String> list = new ObservableList<String>();

我收到一个错误,说我无法实例化ObservableList,因为它是抽象的。可以理解。如果我看看ObservableList的文档,我可以看到SortedList实现ObservableList,但我不能这样做:

I get an error saying I can't instantiate ObservableList because it's abstract. Understandable. If I look at the doc for ObservableList I can see that SortedList implements ObservableList, but I can't do:

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

因为没有适用的构造函数。显然我需要有一个ObservableList传递给SortedList,这是奇怪的,因为我不能创建一个ObservableList。

Because there's no applicable constructors. Apparently I need to have an ObservableList to pass to the SortedList, which is odd because I can't create an ObservableList.

constructor SortedList.SortedList(ObservableList<? extends String>,Comparator<? super String>) is not applicable
  (actual and formal argument lists differ in length)
constructor SortedList.SortedList(ObservableList<? extends String>) is not applicable
  (actual and formal argument lists differ in length)

不知道如何解密。如果我尝试

I'm not sure how to decipher that. If I try

 ObservableList<String> list = new SortedList<SortedList<String>>();
 //or
 ObservableList<String> list = new SortedList<ObservableList<String>>();

出于绝望,我得到一个更加复杂的错误。

out of desperation, I get an even more convoluted error.

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

也不起作用。不知怎么这样(但显然使用不安全的操作):

doesn't work either. Somehow this works (but apparently uses an unsafe operation):

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

所以我试过:

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

但也没有运气。我超级困惑,在一个无尽的循环尝试一遍又一遍地试图理解这一点。我发现的文档显示了没有帮助的示例,或没有示例。官方文档也是没用的:

But no luck there either. I'm super confused, doing the same thigns over and over in an endless loop of trying to understand this. The documentation I've found shows examples that don't help, or no examples. The official documentation is useless too:

例如,假设您有一个集合c,它可能是一个列表,集合或另一种集合。这个成语创建一个新的ArrayList(List接口的实现),最初包含c中的所有元素。

"Suppose, for example, that you have a Collection c, which may be a List, a Set, or another kind of Collection. This idiom creates a new ArrayList (an implementation of the List interface), initially containing all the elements in c.

 List<String> list = new ArrayList<String>(c);


所以要创建ArrayList ,一个List的实现,我需要一个List。我去的文件在第一个地方的原因是学习如何使他们假设我有。我迷路了。帮助?

" So to create ArrayList, an implementation of List, I need to have a List. the reason I went to the documentation in the first place was to learn how to make what they're assuming I have. I'm lost. Help?

推荐答案

使用 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());

这篇关于创建可观察列表/集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆