0

I have multiple controllers in my project, but for now i will specify only 3 (MainController, CarController, ClientController). What i want to do is save all the data that was changed when exiting the program, so basically in stop() method i want to be able to access my CarController & ClientController saveMethods. I dont know if i need a MainController for this; that has references to those previous controllers. Code Samples:

Main:

import Controller.CarController;
import Controller.ClientController;
import Controller.MainController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    public FXMLLoader mainLoader;
    public MainController mainController;

    @Override
    public void start(Stage primaryStage) throws Exception{
        this.mainLoader = new FXMLLoader(getClass().getResource("/View/Main.fxml"));
        Parent root = mainLoader.load();
        this.mainController = this.mainLoader.getController();
        primaryStage.setTitle("Reservation system");
        primaryStage.setScene(new Scene(root, 1000, 600));
        primaryStage.show();
    }

    @Override public void stop(){
        this.mainController.save(); //Should have reference to CarController & ClientController
    }

    public static void main(String[] args) {
        launch(args);
    }
}

MainController:

package Controller;

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

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

public class MainController implements Initializable {
    @FXML
    public CarController carController;
    @FXML
    public ClientController clientController;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    public void setCarController(CarController carController) {
        this.carController = carController;
    }
    public void setClientController(ClientController clientController) {
        this.clientController = clientController;
    }

    public void save(){
        this.clientController.saveClientsToFile();
        this.carController.saveToFile();
    }
}

I dont know if the methods that i am trying to access are important at this point. If you insist i will add them later. I have multiple FXML files with all controllers working seperately. Hope you understand my issue, if not i will try to explain it better if someone responds.

Question: How to access my CarController and ClientController from my MainController?

Rajkishan Swami
  • 3,569
  • 10
  • 48
  • 68
  • 2
    Based on how you named the fields I assume you're using `fx:include`. That means those controllers will be injected into the main controller. The fields are public so you have direct access to them. If that's not the case, or if something is not working, then please provide a [mre] (including the FXML files). – Slaw Dec 10 '19 at 08:35
  • sounds like a duplicate - https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – kleopatra Dec 10 '19 at 10:15

0 Answers0