L
L
lopstick2015-07-15 12:17:57
JavaFX
lopstick, 2015-07-15 12:17:57

Javafx - how to call the rest from the main controller?

Good afternoon!
I am working on a JAVAFX project. There are several tabs.
Each tab has its own form. Created a separate controller for each tab. The tab bar is launched through the main controller. I want to make it so that controllers for each tab are called in the main controller. I can't figure out how to do it at all. Tell me please.
Here is the part of the code to call the forms in the MainApp class

public void initRootLayout() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Shows the tab overview inside the root layout.
     */
    public void showTabOverview() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/TabLayout.fxml"));
            AnchorPane tabOverview = (AnchorPane) loader.load();
            rootLayout.setCenter(tabOverview);

            MainController mainController = loader.getController();
            mainController.setMainApp(this);       
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

In main controller
public class MainController implements Initializable  {
            private MainApp mainApp;
      public MainController() {
      } 
      @Override
      public void initialize(URL location, ResourceBundle resources) {
      System.out.println("App started");	 	  	
      }
      public void setMainApp(MainApp mainApp) {
          this.mainApp = mainApp;  
      }
   }

You need to call UserOverviewController and TeamOverviewController, each tab has its own. In Scene Builder, I separately created a form for UserOverview and TeamOverview, and then imported it into tabs.
UserOverviewController has lines like this
public void setMainApp(MainApp mainApp) {
    	this.mainApp = mainApp;

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Riard Brugekaaim, 2015-08-06
@OLDRihard

Specifically, I didn’t do such tricks on the main window, but I think something similar can be done there.
In particular, declare the necessary tabs in the controller (like the Tab class), specify the loader object of each controller as a child for the desired tab.
Well, or re-declare the AnchorPane located in the tab on (AnchorPane) loader.load(); (Although this is completely crooked)
I will not say that these are optimal solutions, but at least I indicated in which direction to dig.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question