There are dozens of examples with creation of collections via FXCollections
from FXML like this (from here):
<FXCollections fx:factory="observableArrayList">
<String fx:value="A"/>
<String fx:value="B"/>
<String fx:value="C"/>
</FXCollections>
But I can not understand how it works. After all, factory method is no-arg method, so where do String
-elements go? I have thought it is default property, but FXCollections
has no default property. 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? It is not specified in documentation (or I can not find it). All what I found in docs is:
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:
I feel it is stupid question. But could anyone make it clear for me?
Thank you in advance
P.S. Sorry for my english
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?
It doesn't. FXCollections.observableArrayList()
is used to create the list and since the surrounding element to the <String>
elements is a List
, those elements are added to the list. The java code to produce the same result would be
ObservableList list = FXCollections.observableArrayList();
list.add("A");
list.add("B");
list.add("C");
The list is treated the same way read-only list properties are, even though the documentation does not explicitly state this.