Javafx: link fx:id-s
Javafx: link fx:id-s
我有一个 UI 有许多组件可以 重复使用 ;所以,我链接了几个 .fxml
文件。我的 parent .fxml 嵌入了这个:
<fx:include source="Child.fxml" fx:id="first">
<fx:include source="Child.fxml" fx:id="second">
Child.fxml
看起来像这样:
<HBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml">
<Label fx:id="label"/>
<ComboBox fx:id="comboBox"/>
<TextField fx:id="firstTextfield"/>
<TableView fx:id="secondTextfield"/>
</HBox>
Parent.fxml
已定义 fx:controller="ParentController"
。问题是,我如何set/get child 中每个人的数据parent。
喜欢:
first.getLabel().setText("This is the first Label");
first.getComboBox().getValue();
second.getLabel().setText("This is the second Label");
...
请不要建议作为答案 fist.getChildren().get(0)
和其他类似的方法。
我知道我只能定义一个大的.fxml
,然后给每个项目一个id,但我想避免重复代码,我想把它们拆分成更小的组件,这样它可能变得更易于理解,我可以重复使用它们。
您可以将包含的 FXML 的控制器注入到包含它们的 FXML 的控制器中:
public class ParentController {
@FXML
private ChildController firstController ;
@FXML
private ChildController secondController ;
@FXML
private Pane childContainer ; // container holding the included FXMLs
// ...
}
这里我假设 Child.fxml
声明了一个控制器 class fx:controller="ChildController"
。 nested controllers is that they are the .
的字段命名规则
在该控制器中定义适当的数据方法(允许直接访问控件本身通常是不好的做法):
public class ChildController {
@FXML
private Label label ;
@FXML
private ComboBox<String> comboBox ;
// etc...
public void setDisplayText(String text) {
label.setText(text);
}
public String getUserSelectedValue() {
return comboBox.getValue();
}
// ...
}
现在回到 ParentController
,您只需要
first.setDisplayText("This is the first Label");
first.getUserSelectedValue();
second.setDisplayText("This is the second Label");
等等
如果您需要在运行时动态包含 Child.fxml
中定义的更多 FXML 实例,您只需要:
// modify resource name as needed:
FXMLLoader loader = new FXMLLoader(getClass().getResource("Child.fxml"));
Parent childUI = loader.load();
ChildController childController = loader.getController();
childContainer.getChildren().add(childUI);
我有一个 UI 有许多组件可以 重复使用 ;所以,我链接了几个 .fxml
文件。我的 parent .fxml 嵌入了这个:
<fx:include source="Child.fxml" fx:id="first">
<fx:include source="Child.fxml" fx:id="second">
Child.fxml
看起来像这样:
<HBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml">
<Label fx:id="label"/>
<ComboBox fx:id="comboBox"/>
<TextField fx:id="firstTextfield"/>
<TableView fx:id="secondTextfield"/>
</HBox>
Parent.fxml
已定义 fx:controller="ParentController"
。问题是,我如何set/get child 中每个人的数据parent。
喜欢:
first.getLabel().setText("This is the first Label");
first.getComboBox().getValue();
second.getLabel().setText("This is the second Label");
...
请不要建议作为答案 fist.getChildren().get(0)
和其他类似的方法。
我知道我只能定义一个大的.fxml
,然后给每个项目一个id,但我想避免重复代码,我想把它们拆分成更小的组件,这样它可能变得更易于理解,我可以重复使用它们。
您可以将包含的 FXML 的控制器注入到包含它们的 FXML 的控制器中:
public class ParentController {
@FXML
private ChildController firstController ;
@FXML
private ChildController secondController ;
@FXML
private Pane childContainer ; // container holding the included FXMLs
// ...
}
这里我假设 Child.fxml
声明了一个控制器 class fx:controller="ChildController"
。 nested controllers is that they are the
在该控制器中定义适当的数据方法(允许直接访问控件本身通常是不好的做法):
public class ChildController {
@FXML
private Label label ;
@FXML
private ComboBox<String> comboBox ;
// etc...
public void setDisplayText(String text) {
label.setText(text);
}
public String getUserSelectedValue() {
return comboBox.getValue();
}
// ...
}
现在回到 ParentController
,您只需要
first.setDisplayText("This is the first Label");
first.getUserSelectedValue();
second.setDisplayText("This is the second Label");
等等
如果您需要在运行时动态包含 Child.fxml
中定义的更多 FXML 实例,您只需要:
// modify resource name as needed:
FXMLLoader loader = new FXMLLoader(getClass().getResource("Child.fxml"));
Parent childUI = loader.load();
ChildController childController = loader.getController();
childContainer.getChildren().add(childUI);