0

I have a root layout with multiples Tabs. From my main App I open the root layout. There I included multiple FXMLs with their own controllers. I am trying to pass the main controller to one of the Tabes controller.

The issue I am having, everything works as expected, but I get a null exception when I try to click on an action button from the new tab.

RootLayout FXML

<fx:indlue fx:id="myNewTabAnchorPane" source="NewTabFXML.fxml"/>

RootLayout Controller

@FXML NewTabController newTabController;

mainTabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>(){
    @override
    public void change(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue){
        if(newValue == myTab){
        newTabController.setMyRootController(this);
    }

NewTabController

public void setMyRootController(RootController rootController){
this.rootController = rootController;
System.out.println(rootController.getID); // this prints fine
}

However, if I trigger this action I get blank from the same controller

@FXML 
public void createAction(ActionEvent event) throws IOException{
System.out.println(rootController.getID); // with this I get null value. 
}

What am I missing?

Moe
  • 1,427
  • 4
  • 34
  • 54
  • Is it `ntc` or `ect`? Assuming both should be `nct` you do not keep a reference to the controller and you do not use it with `FXMLLoader` which means you're likely using 2 different controller instances: one with the `FXMLLoader` and another one for setting the root controller. This question may help: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Aug 03 '17 at 15:08
  • The only difference is that I have multiple tabs that use different Fxmls that they use their own controllers. So I am not showing a new stage as it's already open. How can I achieve that with my setup? – Moe Aug 03 '17 at 16:08
  • I didn't write anything about stages. If you're refering to the question I linked: It doesn't matter if the content you load is shown in a new stage or not, the approach to passing the information to the controller is the same. – fabian Aug 03 '17 at 17:16

1 Answers1

1

Here is the problem:

 @FXML NewTabController newTabController;

It should be myNewTabAnchorPaneController which is not a partially lowercased class name, but fx:id + Controller concatenation.

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • Thanks so much Sergey! I truly appreciate you help here. I not sure if this is documented someone where, I hope it is. Thanks again! – Moe Aug 04 '17 at 20:11