3

I'm building a Javafx gui application and I'm facing this problem. I do have multiple Scenes for my program and so i have different FXML files and Controller classes. The thing is that in the first "Main Menu" (which is the first that pops up in my app) scene, in the Constructor of the Controller i call some heavy methods loading data from database and more. So what happens it this. In the next scenes i do have "Main Menu Buttons", that switch to Main Menu scene! So every time i go back to the "Main Menu" scene the constructor call the heavy methods loading data. Whereas i don't want that. I don't want to call these methods every time, just Once at the start. Here is some example code simplified :

Main Menu Scene(Controller)

public class MainController {

    @FXML
    Button bt1 = new Button();
    @FXML
    Button bt2 = new Button();
    @FXML
    Button bt3 = new Button();
    public static int choice=0;

    //constructor
    public MainController(){
        try {
           //heavy databse tasks here(loading data)
        }catch (Exception e){
            //error handling
        }


    }

    @FXML
    public void initialize(){}
}

Another Controller class

public class Scene2Controller {

    private Button mainMenu = new Button();

    //constructor
    public Scene2Controller(){}

    @FXML
    public void initialize(){}

    public void goMainMenu(ActionEvent actionEvent) throws IOException {
        Parent menu= FXMLLoader.load(getClass().getResource("/mainScene.fxml"));
        Stage window = (Stage) mainMenu.getScene().getWindow();
        window.getScene().setRoot(menu);
        window.show();
    }
}

So in the Second controller i have a listener method that when buttons clicks it goes back to the main menu scene, loading the appropriate FXML file.

I understand that this seams pretty straight forward to most of you, but im new in javafx and i wanted to know if i there is something doing wrong switching scenes or that i should do different in order for these methods that i have in the mainMenu Constructor class, to run Only Once. Is that obtainable or should i create a Sub-Controller class that run before the Main Menu Scene? Thanks in advance.

Panagiss
  • 3,154
  • 2
  • 20
  • 34
  • 4
    Either: don't reload the FXML every time, just keep a reference to the scene you want to display and re-display it. Or (probably better): move the database loading tasks to a separate class and instantiate that class only once. Pass the (single) instance of that class to whatever controllers need it using techniques in https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D May 19 '20 at 11:21
  • @James_D if i use your second option, where should i instantiate that separate class you say. I mean i could easily then do this work in the Start method in Mainclass. Is that a good practice? – Panagiss May 19 '20 at 13:07
  • @kleopatra i made few edits, do i miss something else? – Panagiss May 19 '20 at 13:08
  • 3
    @Panagiss The responsibility of the main class is to start up the application; either the `start()` or `init()` method would be good places to initialize model data that are used throughout the application. For something similar (but not quite the same), see https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx/32343342#32343342 – James_D May 19 '20 at 13:10
  • 1
    perfect now :)) – kleopatra May 19 '20 at 13:14
  • @James_D So i should perform the actions inside ```start()``` . I will come back with more feedback when i have. Thanks – Panagiss May 19 '20 at 13:24

1 Answers1

0

In my case i had to call few methods, so i just removed them from the first Scene Controller class and add them in the start() method in my MainClass and not inside a Controller class. I think @James_D 's response is the overall best for a similar question like mine one. Thanks !

Panagiss
  • 3,154
  • 2
  • 20
  • 34