1

I am loading a list of FXML files through my Main App. But I am having an issue displaying inside TabPane using its FX:ID. this TabPane is under a borderPane. I am able to access the center of the borderPane but I can't access the TabPane beneath it.

here is my setup

In my start:

     @Override
        public void start(Stage primaryStage) {
            this.primaryStage = primaryStage;
            initRootLayout();
            showPersonOverview();
        }
In my init 

        public void initRootLayout() {
                try {
                    FXMLLoader loader = new FXMLLoader();
                    loader.setLocation(MainApp.class
                            .getResource("view/RootLayout.fxml"));
  //Note this line, I am setting up my rootLayout to be the 
  //mainBorderPane, but I want it to be an AnchorePane under the main
 //BorderPane
                    rootLayout = (BorderPane) loader.load();
                    Scene scene = new Scene(rootLayout);
                    primaryStage.setScene(scene);

                    RootLayoutController controller = loader.getController();
                    controller.setMainApp(this);

                    primaryStage.show();
                } catch (IOException e) {
                    e.printStackTrace();
                }

Here is what I am having an issue with. I can't display this in the TabPane mentioned above.

public void showPersonOverview() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
            AnchorPane personOverview = (AnchorPane) loader.load();
           //Here I am displaying the personOverview inside the main boarderPane
            rootLayout.setCenter(personOverview);

            PersonOverviewController controller = loader.getController();
            controller.setMainApp(this);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

enter image description here

Moe
  • 1,427
  • 4
  • 34
  • 54

1 Answers1

1

Your RootLayoutController has a reference to the tab pane, so define an accessor method to the tab pane from the root controller and use that when you want to add new tabs.

// Store a reference to your root controller in your application.
RootLayoutController rootController;
...

// in initRootLayout()
...
rootController = loader.getController();
...

// in showPersonOverview()
...
AnchorPane personOverview = (AnchorPane) loader.load();
rootController.getTabPane().getTabs().add(
    new Tab("Person Overview", personOverview)
);
...

The rootController is simply:

@FXML tabPane;

public TabPane getTabPane() {
    return tabPane;
}

Alternate solution using node lookups

Alternatively, you can use use node.lookup() functions based upon a css id selector, but, in general, using references like above is probably better over lookups as the references are type safe and not subject to the runtime vagaries of css lookups. CSS lookups sometimes only work as expected after you have added elements to a scene and applied a css and layout pass.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • rootController.getTabPane().getTabs() I don't see a getTabs? do you mean getChildern? we have the @FXML tabPane. that we are getting via getTabPane. getTabs is not populating for me. – Moe Jul 18 '17 at 03:37
  • No, I did not mean `getChildren()`. A `TabPane` has a [`getTabs()`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TabPane.html#getTabs--), method which is what I was using in this example. If you found an alternate solution that works for you, that's great. – jewelsea Jul 18 '17 at 04:09
  • yeap, it's working. it was in front me all this time :) I got a handle of the controller and handle of the TabPane, but i was stuck looking at it and saying what am I going to do with you...... thanks for you help!!! – Moe Jul 18 '17 at 04:13