Sunday, April 16, 2017

JavaFX | Having access of other Controllers from a specific Controller

There are some good ways to do this:
  1. Create a reference of a Controller
  2. private ControllerClass controller;
    
    /*...
    
    Parent root;
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("/path/ofYourFxml.fxml"));
    
    controller = fxmlloader.getController();
    //Now you can use a method like getter and setter to set or get any value of variable
    controller.yourMethodToCall()
    
    Stage stage = new Stage();
    stage.setScene(new Scene(root, 1200, 700));
    stage.show()
    
  3. For dirt and dirty solution: declare any variable you want to use in other class as static (Not recommended for a complex application(static problem))
  4. Use a shared data Model among Controllers
  5. Explaination follows...
    
    

No comments:

Post a Comment