'Setting center of borderpane form another controller class

I have Borderpane as homePane (its fxml file is home.fxml). At first in center of homePane i have a HBox which contains a button called delivery. When the user clicks this button, the center of homePane switchs to display the list of customers. It works !

The code of homeController :

public class HomeController {

@FXML
private BorderPane homePane; 

@FXML
public void deliveryBtnClicked (){
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fragments/customers.fxml"));
    homePane.getChildren().remove(homePane.getCenter());
    try {
        Pane pane = loader.load();
        homePane.setCenter(pane);

    }catch (IOException e){
        System.out.println(e.getMessage());
    }
}

public BorderPane getHomePane() {
    return homePane;
}
}

The new Pane contains the table of customers and a button called order. It has own Controller :

public class CustomerController {
    
    @FXML
    public void loadOrderView() throws IOException {
        Customer customer = customerTable.getSelectionModel().getSelectedItem();
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/home.fxml"));
        loader.load();

        HomeController homeController =  loader.getController();
        BorderPane homePane = homeController.getHomePane();


        homePane.getChildren().remove(homePane.getCenter());
        System.out.println(homePane.getCenter());
        FXMLLoader loader1 = new FXMLLoader(getClass().getResource("/fragments/order.fxml"));
        Pane orderView = loader1.load();
        homePane.setCenter(orderView);
        System.out.println(homePane.getCenter());
     }
} 

When user clicks the button order, the order-view have to be loaded in the center of homePane. the print command shows that the homePane center after removed is null and then after loaded gets new view.

null
AnchorPane@755b1503

But in running Programm its doen't show the order view. I have set onAction of button order to loadOrderView. This is definitive not the problem.



Solution 1:[1]

As James has pointed out, you should not load the home fxml again.

Instead:

  1. Define the orderButton in your controller and in your fxml (via a fx:id and @FXML injection).

  2. Use the scene graph to find the home pane so that you can replace its center.

public class CustomerController {
    
    @FXML Button orderButton
    
    @FXML
    public void loadOrderView() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fragments/order.fxml"));
        Pane orderView = loader.load();
        BorderPane homePane = (BorderPane) orderButton.getScene().getRoot();
        homePane.setCenter(orderView);
     }
} 

An alternate method in a small framework is defined in the answer to:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1