将自定义 FXML 属性设置为自定义 javafx 组件的参数
Set custom FXML properties as parameters for custom javafx component
我创建了自定义组件 TableBlock。它由一个 Label 和 TableView 组成。例如,TableView 可以有 1 到 1000 行。行数由 FXML 文件中的参数 "rowsFromPrefs" 定义。创建 TableView 需要此参数。 TableView 完全由 JAva 代码创建,在 fxml 中只是它的标签和带有行数的参数。
据我所知,JavaFX 在构造FXML 组件时,首先调用构造函数,然后调用@FXML 注解字段,然后启动initialize() 方法。
在我的例子中,当 initialize() 启动时,变量 rowsFromPrefs 仍然为空!但是,如果我尝试从其他线程(不是 JavaFX-launcher)获取 rowsFromPrefs 的值,我会看到它定义了 = "2",就像它应该的那样。
所以我不明白什么时候 Java 从 FXML 文件分配对象参数。如何在创建对象时将参数从 fxml 文件传递给对象。
我看到构造函数参数的 @NamedArg 注释。是创建对象时传递参数的唯一方式吗?
the controller can define an initialize() method, which will be called once on >an implementing controller when the contents of its associated document have >been completely loaded:
TableBlock.java
public class TableBlock extends VBox{
@FXML
private String rowsFromPrefs;
@FXML
private Label label;
public TableBlock() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TableBlock.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void initialize() {
this.table = createTable(rowsFromPrefs);
}
public String getRowsFromPrefs() {
System.out.println("getRowsFromPrefs");
return rowsFromPrefs;
}
public void setRowsFromPrefs(String rowsFromPrefs) {
this.rowsFromPrefs = rowsFromPrefs;
}
}
TableBlock.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import ru.laz.model.controls.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import ru.laz.model.controls.tableblock.*?>
<fx:root type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="Label" />
</children>
</fx:root>
View.java
public class View extends Application {
Parent root = null;
private Scene scene;
@Override
public void init() {
try {
root = FXMLLoader.load(getClass().getResource("View.fxml"));
root.requestLayout();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void start(final Stage stage) throws Exception {
scene = new Scene(root, 640, 480, Color.LIGHTGRAY);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
View.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import ru.laz.model.controls.tableblock.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TableBlock rowsFromPrefs="2" id="IDDQD"/>
</children>
</AnchorPane>
首先,请注意 rowsFromPrefs
上的 @FXML
注释没有任何作用。 @FXML
当当前对象是其控制器的 FXML 文件具有一个具有 fx:id
属性且其值与字段名称匹配的元素时,会导致为该字段注入一个值。因为 TableBlock.fxml
没有包含 fx:id="rowsFromPrefs"
的元素,所以这个注释没有做任何事情。
当正在加载 View.fxml
的 FXMLLoader
遇到 <TableBlock>
元素时,它会通过调用其构造函数创建一个 TableBlock
实例。然后它将设置属性指定的值。所以你的 FXML 元素
<TableBlock rowsFromPrefs="2" id="IDDQD"/>
本质上等同于
TableBlock tableBlock = new TableBlock();
tableBlock.setRowsFromPrefs("2");
tableBlock.setId("IDDQD");
当然,TableBlock
的构造函数只是执行代码所说的操作:它创建一个 FXMLLoader
,为该 FXMLLoader
设置根和控制器,然后调用load()
。 that FXMLLoader
的加载过程将在控制器(正在执行其构造函数的 TableBlock
对象)上设置 @FXML
注入的字段,然后呼叫 initialize()
.
所以 initialize()
作为 TableBlock
构造函数中对 FXMLLoader.load()
调用的一部分被调用;当然这一切都发生在 setRowsFromPrefs("2");
被调用之前。
所以总而言之,TableBlock.initialize()
在 TableBlock.fxml
被解析之后被调用,并且那里定义的任何元素都被注入到它们相应的 @FXML
-注释字段中,但这发生在 View.fxml
已加载。
解决此问题的一种方法是将 rowsFromPrefs
传递给 TableBlock
构造函数。为此,请使用 @NamedArg
annotation:
public class TableBlock extends VBox{
private final String rowsFromPrefs;
@FXML
private Label label;
public TableBlock(@NamedArg("rowsFromPrefs") String rowsFromPrefs) {
this.rowsFromPrefs = rowsFromPrefs ;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TableBlock.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void initialize() {
this.table = createTable(rowsFromPrefs);
}
public String getRowsFromPrefs() {
System.out.println("getRowsFromPrefs");
return rowsFromPrefs;
}
}
现在,您在 FXML 中的属性将被传递给构造函数而不是 set 方法,因此 rowsFromPrefs
将在您调用 fxmlLoader.load()
之前根据需要进行初始化。
当然,另一种选择是将代码从 initialize()
方法移动到 setRowsFromPrefs(...)
方法。如果您打算为每个 TableBlock
实例固定 rowsFromPrefs
,我会使用上述选项,并且仅当您希望能够在生命周期内更改 rowsFromBlocks
时才使用第二个选项一个单独的 TableBlock
个实例。
我创建了自定义组件 TableBlock。它由一个 Label 和 TableView 组成。例如,TableView 可以有 1 到 1000 行。行数由 FXML 文件中的参数 "rowsFromPrefs" 定义。创建 TableView 需要此参数。 TableView 完全由 JAva 代码创建,在 fxml 中只是它的标签和带有行数的参数。
据我所知,JavaFX 在构造FXML 组件时,首先调用构造函数,然后调用@FXML 注解字段,然后启动initialize() 方法。
在我的例子中,当 initialize() 启动时,变量 rowsFromPrefs 仍然为空!但是,如果我尝试从其他线程(不是 JavaFX-launcher)获取 rowsFromPrefs 的值,我会看到它定义了 = "2",就像它应该的那样。
所以我不明白什么时候 Java 从 FXML 文件分配对象参数。如何在创建对象时将参数从 fxml 文件传递给对象。
我看到构造函数参数的 @NamedArg 注释。是创建对象时传递参数的唯一方式吗?
the controller can define an initialize() method, which will be called once on >an implementing controller when the contents of its associated document have >been completely loaded:
TableBlock.java
public class TableBlock extends VBox{
@FXML
private String rowsFromPrefs;
@FXML
private Label label;
public TableBlock() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TableBlock.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void initialize() {
this.table = createTable(rowsFromPrefs);
}
public String getRowsFromPrefs() {
System.out.println("getRowsFromPrefs");
return rowsFromPrefs;
}
public void setRowsFromPrefs(String rowsFromPrefs) {
this.rowsFromPrefs = rowsFromPrefs;
}
}
TableBlock.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import ru.laz.model.controls.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import ru.laz.model.controls.tableblock.*?>
<fx:root type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="Label" />
</children>
</fx:root>
View.java
public class View extends Application {
Parent root = null;
private Scene scene;
@Override
public void init() {
try {
root = FXMLLoader.load(getClass().getResource("View.fxml"));
root.requestLayout();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void start(final Stage stage) throws Exception {
scene = new Scene(root, 640, 480, Color.LIGHTGRAY);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
View.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import ru.laz.model.controls.tableblock.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TableBlock rowsFromPrefs="2" id="IDDQD"/>
</children>
</AnchorPane>
首先,请注意 rowsFromPrefs
上的 @FXML
注释没有任何作用。 @FXML
当当前对象是其控制器的 FXML 文件具有一个具有 fx:id
属性且其值与字段名称匹配的元素时,会导致为该字段注入一个值。因为 TableBlock.fxml
没有包含 fx:id="rowsFromPrefs"
的元素,所以这个注释没有做任何事情。
当正在加载 View.fxml
的 FXMLLoader
遇到 <TableBlock>
元素时,它会通过调用其构造函数创建一个 TableBlock
实例。然后它将设置属性指定的值。所以你的 FXML 元素
<TableBlock rowsFromPrefs="2" id="IDDQD"/>
本质上等同于
TableBlock tableBlock = new TableBlock();
tableBlock.setRowsFromPrefs("2");
tableBlock.setId("IDDQD");
当然,TableBlock
的构造函数只是执行代码所说的操作:它创建一个 FXMLLoader
,为该 FXMLLoader
设置根和控制器,然后调用load()
。 that FXMLLoader
的加载过程将在控制器(正在执行其构造函数的 TableBlock
对象)上设置 @FXML
注入的字段,然后呼叫 initialize()
.
所以 initialize()
作为 TableBlock
构造函数中对 FXMLLoader.load()
调用的一部分被调用;当然这一切都发生在 setRowsFromPrefs("2");
被调用之前。
所以总而言之,TableBlock.initialize()
在 TableBlock.fxml
被解析之后被调用,并且那里定义的任何元素都被注入到它们相应的 @FXML
-注释字段中,但这发生在 View.fxml
已加载。
解决此问题的一种方法是将 rowsFromPrefs
传递给 TableBlock
构造函数。为此,请使用 @NamedArg
annotation:
public class TableBlock extends VBox{
private final String rowsFromPrefs;
@FXML
private Label label;
public TableBlock(@NamedArg("rowsFromPrefs") String rowsFromPrefs) {
this.rowsFromPrefs = rowsFromPrefs ;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TableBlock.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void initialize() {
this.table = createTable(rowsFromPrefs);
}
public String getRowsFromPrefs() {
System.out.println("getRowsFromPrefs");
return rowsFromPrefs;
}
}
现在,您在 FXML 中的属性将被传递给构造函数而不是 set 方法,因此 rowsFromPrefs
将在您调用 fxmlLoader.load()
之前根据需要进行初始化。
当然,另一种选择是将代码从 initialize()
方法移动到 setRowsFromPrefs(...)
方法。如果您打算为每个 TableBlock
实例固定 rowsFromPrefs
,我会使用上述选项,并且仅当您希望能够在生命周期内更改 rowsFromBlocks
时才使用第二个选项一个单独的 TableBlock
个实例。