JavaFX load FXML inside parent controller


Question

I have a borderpane where I'm loading an fxml (alias FirstPanel) with relative controller inside of it and positioned in the center. The fxml contains a TableView and a button which should load another fxml (alias SecondPanel) and relative controller instead the first panel. Basically, the SecondPanel needs to show some details about the data selected in the table.

Is it possible to do it? How can I get the parent of my FirstPanel and use it for the SecondPanel instead of the first?

UPDATE

I've tried many solutions but without reach my goal. The application load the UsersMainPageController which contains only an AnchorPane like parent control, so this is the relative code:

[UsersMainPageController]

public class UsersMainPageController implements Initializable {

    private PostOffice application;

    @FXML
    private AnchorPane ParentControl;

    public void setApp(PostOffice application){
        this.application = application;
    } 

    public void loadPage(String pageName) {
        try {                    
            URL url = getClass().getResource(pageName);
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(url);
            fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
            AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream()); 

            ParentControl.getChildren().clear();///name of pane where you want to put the fxml.
            ParentControl.getChildren().add(page);

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

    public void loadManageUsers () {
        loadPage("UsersManage.fxml");
    }

    public void loadListUsers () {
        loadPage("UsersList.fxml");
    }

    /**
     * Initializes the controller class.
     */

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        loadListUsers();
    }

}

loadListUsers calls UsersList.fxml with the relative controller UsersListController that contains a TableView with some records and some buttons. When I click a specific button, it should call loadManageUsers with relative controller UsersManageController which contains some fields for editing data and inserting new users. When users are edited or inserted, it should be able to return to the previous page with the TableView and clear the current page (in this case UsersManageController).

[UsersListController]

public class UsersListController implements Initializable {
    private UsersMainPageController mainController;

    @FXML
    private void handleButtonEditAction(ActionEvent event) throws IOException {
        mainController.loadManageUsers();
    }
}

[UsersManageController]

public class UsersManageController implements Initializable {
    private UsersMainPageController mainController;

    @FXML
    private void handleButtonBackAction(ActionEvent event) throws IOException {
    mainController.loadListUsers();
    }
}

When I click from the UsersListController the ButtonEdit to load the UsersManageController, I get this error:

Caused by: java.lang.NullPointerException
    at postoffice.multiuser.UsersListController.handleButtonAggiornaAction(UsersListController.java:210)
    ... 50 more
1
0
11/10/2013 11:06:41 AM

you can add your child fxml into parent controller..

1.first just take a anchor pane and set its bounds where you want to put your FXML code.

now try this...

 button.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
                openNewWindow("Example.fxml");
        }
    });

you can pass fxml name into function...

public void openNewWindow(String FXMLFile)
 {
     //ChildNode child;
    try {                    
        URL url = getClass().getResource(FXMLFile);
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(url);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream()); 

        anchor.getChildren().clear();///name of pane where you want to put the fxml.
        anchor.getChildren().add(page);
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
 }
0
11/5/2013 6:11:57 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon