0

I have two scenes, one who serves as a main scene and one who serves as a wizard.

When the user clicks a button on the wiz scene, I want a new tab added to the tabpane in the main scene.

The problem is that the program thinks I am referencing to a tabPane in the wizard scene, while it's actually placed in the main scene.

So my question is, how can I reference to elements in another scene? (actually another FXML document)

Note: I am using scene builder

By the way, I got it to work using : How do I access a UI element from another controller class in JavaFX?

But I understand this isn't the recommended approach, so what is?

It could be that I am approaching this all wrong.. But all I want is a new window opening with a button inside, and when the user press the button inside the new window, a new tab should be added to the main window.

Community
  • 1
  • 1
miniHessel
  • 778
  • 4
  • 15
  • 39

1 Answers1

0

Original content Source:

JavaFX 8 Tutorial - Part 2: Model and TableView


From this tutorial about JavaFX, add a method in your controller:

/**
 * Is called by the main application to give a reference back to itself.
 * 
 * @param mainApp
 */
public void setMainApp(MainApp mainApp) {
    this.mainApp = mainApp;

    // Add observable list data to the table
    personTable.setItems(mainApp.getPersonData());
}

Then do as follows:

Connecting MainApp with the PersonOverviewController

The setMainApp(...) method must be called by the MainApp class. This gives us a way to access the MainApp object and get the list of Persons and other things. Replace the showPersonOverview() method with the following. It contains two additional lines:

MainApp.java - new showPersonOverview() method

/**
 * Shows the person overview inside the root layout.
 */
public void showPersonOverview() {
    try {
        // Load person overview.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
        AnchorPane personOverview = (AnchorPane) loader.load();

        // Set person overview into the center of root layout.
        rootLayout.setCenter(personOverview);

        // Give the controller access to the main app.
        PersonOverviewController controller = loader.getController();
        controller.setMainApp(this);

    } catch (IOException e) {
        e.printStackTrace();
    }
}
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
incises
  • 1,045
  • 9
  • 7