Javafx 如何从对子fxml组件执行的操作更改父fxml内容?

Javafx 如何从对子fxml组件执行的操作更改父fxml内容?,javafx,fxml,Javafx,Fxml,我创建了JavaFX应用程序,并希望通过层次结构和MVC结构在不同的FXML之间分发完整的功能 在开始时,RoolLayout.fxml是加载,它是父fxml RootLayout.fxml 从这个MainMenuBar.fxml文件内部也加载了子fxml文件 MainMenuBar.fxml 现在,从控制器addPatient(ActionEvent事件)中选择菜单项addPatiendMenuItem时,将调用方法。 通过此方法,我如何更改AnchorPane fx:id=“dashboar

我创建了JavaFX应用程序,并希望通过层次结构和MVC结构在不同的FXML之间分发完整的功能

在开始时,RoolLayout.fxml是加载,它是父fxml

RootLayout.fxml 从这个MainMenuBar.fxml文件内部也加载了子fxml文件

MainMenuBar.fxml 现在,从控制器addPatient(ActionEvent事件)中选择菜单项addPatiendMenuItem时,将调用方法。 通过此方法,我如何更改AnchorPane fx:id=“dashboard”,它是父fxml(RootLayout.fxml)文件的组件

假设我想在这个AnchorPane中加载第三个fxml(即Dashboard.fxml)的内容,我该怎么做


我花了很多时间来寻找如何从对子控制器组件执行的操作中更改父控制器组件?

MainMenuBarController
中创建一个表示正在更改的状态的属性(“查看状态”)。这一点是否有意义取决于您对应用程序有更多的了解,但您可能会执行以下操作

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);
    }
}
然后在“父”控制器中执行

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...
        }
    });
 }
}
根据应用程序逻辑,您可能需要不同的属性,例如在
main菜单下的ubarcontroller
define中

ObjectProperty<Node> display = new SimpleObjectProperty<>();
ObjectProperty display=newsimpleobjectproperty();
它将存储
RootLayoutController
应该显示的节点。结构类似,在
addPatient
handler方法中设置该属性,并在
RootLayoutController

public class MainMenuBarController {

 @FXML
 private MenuItem addPatiendMenuItem;

 @FXML
 private MenuItem findPatientMenuItem;

 @FXML
 public void closeApplication(){
    System.exit(0);
 }

 @FXML
 public void addPatient(ActionEvent event){

 }
}
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);
    }
}
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...
        }
    });
 }
}
ObjectProperty<Node> display = new SimpleObjectProperty<>();