「Pro JavaFX 8: デスクトップ、モバイル、および組み込み Java クライアントを構築する決定的なガイド」の第 3 章では、FXML ファイルでオブジェクトを直接指定する方法を例で示しています。
完全な FXML ファイルは、この投稿の最後に、例の他のファイルと共に記載されています。
これが私が取っているスニペットです。このsizes
フィールドはfx:factory
属性を使用して、ファクトリ メソッド Utilities.createList() を使用して整数のリストを作成する必要があることを示します。このリストには、3 つの整数が入力されます。
<sizes>
<Utilities fx:factory="createMyCollection">
<Integer fx:value="1"/>
<Integer fx:value="2"/>
<Integer fx:value="3"/>
</Utilities>
</sizes>
Utilities.java は次のとおりです。
package projavafx.fxmlbasicfeatures;
import java.util.ArrayList;
import java.util.List;
public class Utilities {
public static final Double TEN_PCT = 0.1d;
public static final Double TWENTY_PCT = 0.2d;
public static final Double THIRTY_PCT = 0.3d;
public static List<Integer> createList() {
return new ArrayList<>();
}
}
私の質問は、これらのファクトリ メソッドの使用に関係する一般的なメカニズムは何ですか?
メソッドを使用して作成されたオブジェクトに 3 つの整数を追加する必要があることを FXMLLoader がどのように認識しているかを理解したいと思いますadd
。当然、それはおそらくについて何らかの形で知っているに違いありませんが、その知識はどこに指定されているのでしょうか。FXMLLoaderに組み込まれていますか? もしそうなら、そのようなファクトリメソッドをユーザー定義クラスにどのように提供できますか?List
Collection
実際にユーザー定義クラスで使ってみました。次のスニペットを Utilities.java に追加しました。これにより、MyCollection
単一のメソッドを持ち、メソッドadd(Integer)
を定義するクラスが作成されますUtilities.createMyCollection
。
public class Utilities {
(...)
public static class MyCollection {
private List<Integer> myList = new LinkedList<>();
public void add(Integer o) {
myList.add(o);
}
public String toString() {
return myList.toString();
}
}
public static MyCollection createMyCollection() {
return new MyCollection();
}
(...)
}
ただし、FXML ファイルで createMyCollection を置換すると、「MyCollections にはデフォルト プロパティがありません。MyCollection コンテンツをプロパティ要素に配置してください。」というメッセージが表示されました。
これは、ユーザー定義クラスのデフォルト プロパティをどのように宣言できるのか、またどのようにList
既に持っているのか疑問に思います。
すべてのファイルは次のとおりです (上記の Utilities.java を除く)。
FXMLBasicFeatures.fxml:
<?import javafx.scene.paint.Color?>
<?import projavafx.fxmlbasicfeatures.FXMLBasicFeaturesBean?>
<?import projavafx.fxmlbasicfeatures.Utilities?>
<?import java.lang.Double?>
<?import java.lang.Integer?>
<?import java.lang.Long?>
<?import java.util.HashMap?>
<?import java.lang.String?>
<FXMLBasicFeaturesBean name="John Smith"
flag="true"
count="12345"
xmlns:fx="http://javafx.com/fxml/1">
<address>12345 Main St.</address>
<foreground>#ff8800</foreground>
<background>
<Color red="0.0" green="1.0" blue="0.5"/>
</background>
<price>
<Double fx:value="3.1415926"/>
</price>
<discount>
<Utilities fx:constant="TEN_PCT"/>
</discount>
<sizes>
<Utilities fx:factory="createList">
<Integer fx:value="1"/>
<Integer fx:value="2"/>
<Integer fx:value="3"/>
</Utilities>
</sizes>
<profits>
<HashMap q1="1000" q2="1100" q3="1200" a4="1300"/>
</profits>
<fx:define>
<Long fx:id="inv" fx:value="9765625"/>
</fx:define>
<inventory>
<fx:reference source="inv"/>
</inventory>
<products>
<String fx:value="widget"/>
<String fx:value="gadget"/>
<String fx:value="models"/>
</products>
<abbreviations CA="California" NY="New York" FL="Florida" MO="Missouri"/>
</FXMLBasicFeaturesBean>
FXMLBasicFeaturesBean.java:
package projavafx.fxmlbasicfeatures;
import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FXMLBasicFeaturesBean {
private String name;
private String address;
private boolean flag;
private int count;
private Color foreground;
private Color background;
private Double price;
private Double discount;
private List<Integer> sizes;
private Map<String, Double> profits;
private Long inventory;
private List<String> products = new ArrayList<String>();
private Map<String, String> abbreviations = new HashMap<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Color getForeground() {
return foreground;
}
public void setForeground(Color foreground) {
this.foreground = foreground;
}
public Color getBackground() {
return background;
}
public void setBackground(Color background) {
this.background = background;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Double getDiscount() {
return discount;
}
public void setDiscount(Double discount) {
this.discount = discount;
}
public List<Integer> getSizes() {
return sizes;
}
public void setSizes(List<Integer> sizes) {
this.sizes = sizes;
}
public Map<String, Double> getProfits() {
return profits;
}
public void setProfits(Map<String, Double> profits) {
this.profits = profits;
}
public Long getInventory() {
return inventory;
}
public void setInventory(Long inventory) {
this.inventory = inventory;
}
public List<String> getProducts() {
return products;
}
public Map<String, String> getAbbreviations() {
return abbreviations;
}
@Override
public String toString() {
return "FXMLBasicFeaturesBean{" +
"name='" + name + '\'' +
",\n\taddress='" + address + '\'' +
",\n\tflag=" + flag +
",\n\tcount=" + count +
",\n\tforeground=" + foreground +
",\n\tbackground=" + background +
",\n\tprice=" + price +
",\n\tdiscount=" + discount +
",\n\tsizes=" + sizes +
",\n\tprofits=" + profits +
",\n\tinventory=" + inventory +
",\n\tproducts=" + products +
",\n\tabbreviations=" + abbreviations +
'}';
}
}
FXMLBasicFeaturesMain.java:
package projavafx.fxmlbasicfeatures;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
public class FXMLBasicFeaturesMain {
public static void main(String[] args) throws IOException {
FXMLBasicFeaturesBean bean = FXMLLoader.load(
FXMLBasicFeaturesMain.class.getResource(
"/projavafx/fxmlbasicfeatures/FXMLBasicFeatures.fxml")
);
System.out.println("bean = " + bean);
}
}