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