简体   繁体   中英

JAVAFX - FXML - Access Loaded FXML Controls from Parent Controller

Here is what I am trying to accomplish.

/Package A/
/Package A/ApplicationController.java
/Package A/Application.fxml

In my Application.fxml file I have a button, and when that button is clicked it loads the following "MyGrid.fxml" file.

/Package B/
/Package B/MyGrid.fxml (has a label #mygridlabelid

The code I am using is:

ContentPane.getChildren().add((Node)FXMLLoader.load(getClass().getResource("/Package B/MyGrid.fxml")));

But the problem is.. even though I am loading the MyGrid.fxml file from the ApplicationController, I cannot access #mygridlabelid from the ApplicationController file. I defined @FXML label mygridlabelid in the ApplicationController.java file, but it doesn't get instantiated :(

How can I do that? Any tricks or hacks around it?

I managed to solve the problem by doing the following... and make sure that the .fxml file does not have fx:controller set. Or else you will run into "Controller value already specified."

    FXMLLoader loader = new FXMLLoader(getClass().getResource("/your.fxml"));
    loader.setController(this);
    try {
        ContentPane.getChildren().add((Node)loader.load());
    } catch (IOException e){
        System.out.println(e.getMessage());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM