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.
You can navigate the scene graph as shown in another answer, and this is a perfectly good solution for a small application.
For larger applications, where you might need more flexibility in terms of the ability to change, e.g., how the layout is managed, consider using a MVC/MVVM type of approach. Start with a model class that maintains the current view state:
Make your controllers implement an interface that allows them to have a reference to a
ViewModel:Your
HomeController, assuming it is loaded once and the home page is always shown, can create a single instance of theViewModel, observe it, update the border pane, and pass the instance to other controllers when they are loaded:Then make the controllers for the FXML "fragments" implement
ViewModelObserver. They can simply update the current view, and the home controller will load and display it:You may need to include other properties in your view model, e.g. to give access to the
Customerinstance etc.