0

Link to tutorial explaining the code. https://www.youtube.com/watch?v=5GsdaZWDcdY

Overview:

class ScreensController extends StackPane //keep pane so we can remove add screens on top/bottom

It has this method for loading multiple screens.

public boolean loadScreen(String name, String resource) {
    try {                            //fxml file
        FXMLLoader myLoader = new   FXMLLoader(getClass().getResource(resource));
        //System.out.println(resource);
        Parent loadScreen = (Parent) myLoader.load();//class cast to controlled screen

        ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());//Returns the controller associated with the root object.
        //inject screen controllers to myscreencontroller
        myScreenControler.setScreenParent(this);// inject screen controllers to each screen here
        addScreen(name, loadScreen);
        return true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return false;
    }
}

Each screen controller has this object

ScreensController myController;

And this method for switching between screens (switch which controller is in control)

@FXML
private void goToScreen2(ActionEvent event){
    myController.setScreen(ScreensFramework.screen2ID);
}

public interface ControlledScreen {

//This method will allow the injection of the Parent ScreenPane
public void setScreenParent(ScreensController screenPage);
}

This is a very brief synopsis and I'm sure I'm leaving out integral parts of the code. My question is basically How can I send something (anything) from one screen to another? I'd also like to be able to have a universal model object that all the controllers can access and control. If you take a look at my code here You will see I am putting all my model objects into the "initialize" method of each SEPARATE controller. If any of you can give me some pointers or feedback on my code I would really appreciate it. Thanks.

user3277087
  • 49
  • 1
  • 5
  • Maybe see http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310. You probably want to make your model a property of the `ScreensController` and then add a method to the `ControlledScreen` interface `setModel(Model model);` for passing the model to the controlled screens. Then the model acts as a holder for the data, so one controller can modify it and another will see the changes. – James_D Feb 13 '16 at 19:22
  • In addition to what @James_D said, you can make use of the static keyword in a parent class that both controllers inherit from. – JohnRW Feb 14 '16 at 00:47

0 Answers0