0

I am dealing with JavaFX, as you can see in the picture below, I have a menu (A,B,C,D,E) on the left, all I want to do is that when I click on the menu item, the content of the page change (the red form).

I implemented my code, it works fine visually, but at each time it creates a new controller for each new form. And when it does so I loose the data of my main frame. Here is my Code to explain better.

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 (the controller of the red form)

      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) {



            }
        }

I hope that I clarified well the problem, any help? I am blocked here for two days :v enter image description here

Bashir
  • 2,057
  • 5
  • 19
  • 44
  • Please provide a [mcve] that demonstrates the problem (mind the __M__ please, no need to show more than 2 items to switch between) and don't forget the fxml for all panes. And beware: you _must not_ use the same controller (as you already were advised in your last question) - it's technically possible but the outcome is .. unexpected as you have seen :) – kleopatra Jul 18 '19 at 11:52
  • Another beware: periods are not specified to be allowed in the parameter for grabbing a resource! You might get away with it in certain IDEs, but it will most certainly blow after deploy. – kleopatra Jul 18 '19 at 11:54
  • 1
    by the sound of it, I suspect that your real problem is [passing parameters between controllers](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) - if so, this might be closed as duplicate – kleopatra Jul 18 '19 at 11:57
  • https://github.com/sedj601/RestaurantOrdersDuplicateFX – SedJ601 Jul 18 '19 at 13:29
  • Try [this](https://stackoverflow.com/questions/14370183/passing-parameters-to-a-controller-when-loading-an-fxml) or [This](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx). I suggest you learn the second one. – SedJ601 Jul 18 '19 at 13:32
  • Possible duplicate of [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – SedJ601 Jul 18 '19 at 13:32

0 Answers0