通过 FXCollections 从 FXML 创建集合
Creation of collection via FXCollections from FXML
有许多通过 FXCollections
从 FXML 创建集合的示例(来自 here):
<FXCollections fx:factory="observableArrayList">
<String fx:value="A"/>
<String fx:value="B"/>
<String fx:value="C"/>
</FXCollections>
但我不明白它是如何工作的。毕竟工厂方法是 no-arg 方法,那么 String
-elements 到哪里去了呢?我一直认为它是默认的 属性,但是 FXCollections
没有默认的 属性。 observableArrayList()
工厂方法有一个重载版本,它在文档和源代码中获取集合元素的可变参数。但是为什么FXML
要用这个方法呢?它没有在文档中指定(或者我找不到它)。我在文档中找到的所有内容是:
The fx:factory attribute is another means of creating objects whose classes do not have a default constructor. The value of the attribute is the name of a static, no-arg factory method for producing class instances. For example, the following markup creates an instance of an observable array list, populated with three string values:
我觉得这是个愚蠢的问题。但是谁能帮我说清楚吗?
提前致谢
P.S。对不起我的英语
There is overloaded version of observableArrayList()
factory method which takes vararg of collection's elements in docs and source code. But why does FXML use this method?
没有。 FXCollections.observableArrayList()
用于创建列表,并且由于 <String>
元素的周围元素是 List
,这些元素被添加到列表中。产生相同结果的 java 代码将是
ObservableList list = FXCollections.observableArrayList();
list.add("A");
list.add("B");
list.add("C");
列表的处理方式与 read-only list properties 相同,即使文档没有明确说明这一点。
有许多通过 FXCollections
从 FXML 创建集合的示例(来自 here):
<FXCollections fx:factory="observableArrayList">
<String fx:value="A"/>
<String fx:value="B"/>
<String fx:value="C"/>
</FXCollections>
但我不明白它是如何工作的。毕竟工厂方法是 no-arg 方法,那么 String
-elements 到哪里去了呢?我一直认为它是默认的 属性,但是 FXCollections
没有默认的 属性。 observableArrayList()
工厂方法有一个重载版本,它在文档和源代码中获取集合元素的可变参数。但是为什么FXML
要用这个方法呢?它没有在文档中指定(或者我找不到它)。我在文档中找到的所有内容是:
The fx:factory attribute is another means of creating objects whose classes do not have a default constructor. The value of the attribute is the name of a static, no-arg factory method for producing class instances. For example, the following markup creates an instance of an observable array list, populated with three string values:
我觉得这是个愚蠢的问题。但是谁能帮我说清楚吗?
提前致谢
P.S。对不起我的英语
There is overloaded version of
observableArrayList()
factory method which takes vararg of collection's elements in docs and source code. But why does FXML use this method?
没有。 FXCollections.observableArrayList()
用于创建列表,并且由于 <String>
元素的周围元素是 List
,这些元素被添加到列表中。产生相同结果的 java 代码将是
ObservableList list = FXCollections.observableArrayList();
list.add("A");
list.add("B");
list.add("C");
列表的处理方式与 read-only list properties 相同,即使文档没有明确说明这一点。