2

「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に組み込まれていますか? もしそうなら、そのようなファクトリメソッドをユーザー定義クラスにどのように提供できますか?ListCollection

実際にユーザー定義クラスで使ってみました。次のスニペットを 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);
    }
}
4

1 に答える 1

4

実際には、ここでいくつかの異なる問題が発生しています。ご存じのように、基本的な使用法は、 がFXMLLoaderJavaBean 命名スキームを介して従来のスタイルのプロパティを検索することです。だからクラスがあるなら

public class Bean {

    private String text ;

    public void setText(String text) {
        this.text = text ;
    }

    public String getText() {
        return text ;
    }
}

Bean次に (クラスにはデフォルトの引数なしのコンストラクターがあるため)、 FXML でインスタンス化できます。

<Bean>

プロパティを属性としてsetText参照することで、メソッドを呼び出すことができます。text

<Bean text="Some text"/>

またはプロパティ要素として:

<Bean>
    <text>
        <String fx:value="Some text"/>
    </text>
</Bean>

java.util.List特別扱いを受ける例。プロパティ名が読み取り専用Listプロパティ(つまり、メソッドjava.util.Listはあるがget...メソッドがないタイプのプロパティ) と一致する場合、FXML の子ノードは対応するインスタンスメソッドset...に渡されます。Listadd(...)

したがって、そのようなプロパティを に追加するとBean:

import java.util.List ;
import java.util.ArrayList ;

public class Bean {

    private String text ;

    private List<String> elements ;

    public Bean() {
        this.elements = new ArrayList<>();
    }

    public List<String> getElements() {
        return elements ;
    }

    public void setText(String text) {
        this.text = text ;
    }

    public String getText() {
        return text ;
    }
}

次に、FXML でリストを作成できます。

<Bean text="Some text">
  <elements>
    <String fx:value="One"/>
    <String fx:value="Two"/>
    <String fx:value="Three"/>
  </elements>
<Bean>

あなたが参照する他の問題は、「デフォルトのプロパティ」です。class@DefaultPropertyのアノテーションを使用し、デフォルトと見なされるプロパティの名前を指定することで、クラスのデフォルト プロパティを指定できます。

import java.util.List ;
import java.util.ArrayList ;

@DefaultProperty("text")
public class Bean {

    private String text ;

    private List<String> elements ;

    public Bean() {
        this.elements = new ArrayList<>();
    }

    public List<String> getElements() {
        return elements ;
    }

    public void setText(String text) {
        this.text = text ;
    }

    public String getText() {
        return text ;
    }
}

プロパティを指定せずに FXML でインスタンス要素の子要素を<Bean>指定すると、それらがデフォルト プロパティの値として使用されます。

<Bean>
  <String fx:value="Some Text"/>
</Bean>

setText("Some Text")インスタンスで呼び出しBeanます。

そしてもちろん、これらのアイデアを組み合わせて、Listインスタンスをデフォルト プロパティにすることもできます (これは基本的に、レイアウト コンテナーがどのように機能するかです: デフォルト プロパティとしてPane定義します)。"children"

import java.util.List ;
import java.util.ArrayList ;

@DefaultProperty("elements")
public class Bean {

    private String text ;

    private List<String> elements ;

    public Bean() {
        this.elements = new ArrayList<>();
    }

    public List<String> getElements() {
        return elements ;
    }

    public void setText(String text) {
        this.text = text ;
    }

    public String getText() {
        return text ;
    }
}

そして今、あなたはできる

<Bean text="Some Text">
  <String fx:value="One"/>
  <String fx:value="Two" />
  <String fx:value="Three" />
</Bean>

elementsリストにが入力され["One", "Two", "Three"]ます。

于 2015-05-16T13:58:36.573 に答える