我创建了JavaFX应用程序,并希望在具有层次结构和MVC结构的不同FXML之间分发完整的功能。
在开始时RoolLayout.fxml是父FXML的加载
<BorderPane prefHeight="400.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.nikunj.drclinic.controller.RootLayoutController">
<center>
<AnchorPane fx:id="dashboard" BorderPane.alignment="CENTER" />
</center>
<top>
<HBox BorderPane.alignment="CENTER">
<children>
<fx:include fx:id="mainMenu" source="MainMenuBar.fxml" />
</children>
</HBox>
</top>
</BorderPane>
对于这个使用过的控制器是RootLayoutController.java
public class RootLayoutController {
@FXML
private MainMenuBarController mainMenuBarController;
@FXML
private AnchorPane dashboard;
@FXML
private void initialize() {
// Initialize the person table with the two columns.
}
}
从内部加载MainMenuBar.fxml文件,这是子fxml文件
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.nikunj.drclinic.controller.MainMenuBarController">
<children>
<MenuBar layoutY="2.0" prefWidth="1000.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#closeApplication" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Patient Detail">
<items>
<MenuItem fx:id="addPatiendMenuItem" mnemonicParsing="false" onAction="#addPatient" text="Add Patient" />
<MenuItem mnemonicParsing="false" text="Find Patient" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
此MainMenuBar.fxml的控制器文件是MainMenuBarController.java
public class MainMenuBarController {
@FXML
private MenuItem addPatiendMenuItem;
@FXML
private MenuItem findPatientMenuItem;
@FXML
public void closeApplication(){
System.exit(0);
}
@FXML
public void addPatient(ActionEvent event){
}
}
现在从控制器选择菜单项addPatiendMenuItem调用addPatient(ActionEvent事件)方法。 从这个方法我怎么能改变AnchorPane fx:id =&#34; dashboard&#34;它是父fxml(RootLayout.fxml)文件的组成部分。
假设我想在此AnchorPane中加载第三个fxml(即Dashboard.fxml)的内容,我该怎么做?
我花了很多时间来查找,如何从子控制器组件上执行的操作中更改父控制器组件?
答案 0 :(得分:1)
在MainMenuBarController
中创建一个表示您要更改的状态的属性(&#34;查看状态&#34;)。让它变得有意义取决于对应用程序的了解更多,但你可能会做类似
public class MainMenuBarController {
private final BooleanProperty addPatientRequested = new SimpleBooleanProperty();
public BooleanProperty addPatientRequestedProperty() {
return addPatientRequested ;
}
public final boolean isAddPatientRequested() {
return addPatientRequestedProperty().get();
}
public final boolean setAddPatientReqested(boolean requested) {
addPatientReqestedProperty().set(requested);
}
@FMXL
private void addPatient(ActionEvent event) {
setAddPatientRequested(true);
}
}
然后在&#34;父母&#34;控制器
public class RootLayoutController {
@FXML
private MainMenuBarController mainMenuBarController;
@FXML
private AnchorPane dashboard;
@FXML
private void initialize() {
// Initialize the person table with the two columns.
mainMenuBarController.addPatientRequestedProperty().addListener((obs, wasRequested, isNowRequested) -> {
if (isNowRequested) {
// code to execute...
}
});
}
}
根据您的应用程序逻辑,您可能需要不同的属性,例如在MainMenuBarController
定义
ObjectProperty<Node> display = new SimpleObjectProperty<>();
将存储RootLayoutController
应显示的节点。结构类似,在addPatient
处理程序方法中设置该属性并在RootLayoutController