切换fxml页面而不创建新控制器

时间:2019-07-18 09:23:37

标签: java javafx controller fxml

我正在处理JavaFX,如下图所示,左侧有一个菜单(A,B,C,D,E),我要做的就是单击菜单时项目,页面内容更改(红色形式)。

我实现了我的代码,它在视觉上运行良好,但是每次它为每种新表单创建一个新的控制器。当这样做的时候,我松开了主机的数据。这是我的代码,可以更好地解释。

MainFrame.fxml

<ScrollPane xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.Inhumer.MainController" >

        <BorderPane fx:id="myContent" >
         <center >
                 <fx:include fx:id="demandeur" source="Demandeur.fxml" />
         </center>
         <left>
             <fx:include fx:id="menu" source="SideBar_Inhumer.fxml" />

         </left>
        </BorderPane>
</ScrollPane>

MainController.java

package Controller.Inhumer;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;

import java.io.IOException;
import java.util.HashMap;

public class MainController {
    HashMap<String, Parent> menuItems;
    @FXML
    public BorderPane myContent ;

    @FXML
    DemandeurController demandeurController;
    @FXML
    MenuController menuController;

    @FXML public void initialize() throws IOException {
/*load the content of my forms */
        Parent rootDemandeur = new FXMLLoader(getClass().getResource("../../View/Inhumer/Demandeur.fxml")).load();
        Parent rootDefunt =  new FXMLLoader(getClass().getResource("../../View/Inhumer/Defunt.fxml")).load();
        Parent rootEmplacement =  new FXMLLoader(getClass().getResource("../../View/Inhumer/Emplacement.fxml")).load();
        Parent rootPrestataire =  new FXMLLoader(getClass().getResource("../../View/Inhumer/Prestataire.fxml")).load();
        Parent rootOperation =  new FXMLLoader(getClass().getResource("../../View/Inhumer/Operation.fxml")).load();

        menuItems = new HashMap<>();
        menuItems.put("demandeur",rootDemandeur); //A
        menuItems.put("defunt",rootDefunt); //B
        menuItems.put("emplacement",rootEmplacement); //C
        menuItems.put("prestataire",rootPrestataire); //D
        menuItems.put("operation",rootOperation); //E

        System.out.println("Application started");
        demandeurController.init(this);
        menuController.init(this);
    }

}

MenuController.java

package Controller.Inhumer;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;

import java.io.IOException;

public class MenuController  {

    private MainController main;
    /*methods called onClick on each item of my menu*/
    @FXML
    public void goToDemandeur() throws IOException {
        main.myContent.setCenter(main.menuItems.get("demandeur"));
    }

    @FXML
    public void goToDefunt() throws IOException {        
        main.myContent.setCenter(main.menuItems.get("defunt"));

    }

    @FXML
    public void goToEmplacement() throws IOException {
        main.myContent.setCenter(main.menuItems.get("emplacement"));

    }

    @FXML
    public void goToPrestataire() throws IOException {
        main.myContent.setCenter(main.menuItems.get("prestataire"));

    }

    @FXML
    public void goToOperation() throws IOException {
        main.myContent.setCenter(main.menuItems.get("operation"));

    }



    public void init(MainController mainController) {
        main = mainController;
    }
}

DemandeurController.java(红色形式的控制器)

      import javafx.fxml.FXML;
        import javafx.fxml.Initializable;

        import java.io.IOException;
        import java.net.URL;
        import java.util.ResourceBundle;

        public class DemandeurController implements Initializable {
            public MainController mainController;


        /*Onclick action on the button called "suivant"*/


             @FXML
                public void next() throws IOException {
                    System.out.println(mainController);
                    /*print null, because when loading this form mainController will get
         null value, and I want to always get the value of mainController 
so I can access to its content from this controller*/
                }

                public void init(MainController mainController) {
                this.mainController = mainController;
            }

            @Override
            public void initialize(URL location, ResourceBundle resources) {



            }
        }

我希望我能很好地阐明问题,对您有帮助吗?我在这里被封锁了两天:v enter image description here

0 个答案:

没有答案