http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htmの説明に従ってカスタムJavaFXコンポーネントを作成しました。カスタムjavafxコンポーネントの属性としてカスタムタイプのFXMLプロパティを設定します
// Component.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml">
<Label fx:id="label"/>
</fx:root>
// Component.java
public class Component extends VBox {
private final Entity entity;
@FXML private Label label;
public Component(@NamedArg("entity") Entity entity) {
this.entity = entity;
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(Component.class.getResource("/Component.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
@FXML
private void initialize() {
label.textProperty().set(this.entity.toString());
}
}
私はルートレイアウト(カスタムコンポーネントも)にインポートします。
// Root.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import foo.bar.javafx.Component?>
<?import javafx.scene.layout.AnchorPane?>
<fx:root type="javafx.scene.layout.AnchorPane"xmlns:fx="http://javafx.com/fxml">
// TODO How to pass entity?
<Component entity=?>
</Component>
// Root.java
public class Root extends AnchorPane {
public Root() {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(Root.class.getResource("/Root.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
}
どのように私は、コンポーネントにエンティティを渡すことができますか? 文字列を渡す方法を知っています - それはちょうど<Component entity="text">
です。しかし、どのように任意のクラスのインスタンスを渡すには? P.P.このためにシングルトンパターンを使用したくありません。
'entity'タグを使うには' setEntity'メソッドが必要ですが、 'Entity'はfinalですが、それはできません。なぜあなたはこれをやりますか?あなたは何をしたいのですか? – Sunflame
@Sunflameこれでビューとモデルを分解したいと思います。エンティティはモデルです。コンポーネントビューに関連しています。私の主なメソッドでは、私はモデル(Entityクラス)をインスタンス化し、ビュー(Component)に渡したいと思います。 – McMerphy
質問はあなたのモデルをどうやって得るのですか?あなたのモデルで何をしたいですか? – Sunflame