Same Stage, Different Fxml - Javafx


Question

I am new to JavaFX. I have a window loaded with vertical split pane. Here left side of the split page I have couple of buttons. On Each button click I need to load separate fxml on the right side of the split pane. So here I paste screen shot to be clear.

enter image description here

Here as of now I am opening in separate stage, separate scene when search button is hit. Now I need to load Searcher in the right side of baselayout window. Here is some code which loads baseLayout.

@Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Base Layout");
        BaseController.setMlTool(this);
        FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Base.fxml"));
        baseLayout = (AnchorPane) loader.load();
        Scene scene = new Scene(baseLayout);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

Here is some code which loads Searcher on button click.

@FXML
    private void initialize(){
        System.out.println("Testing");
    }

    @FXML
    private void handleSearchButton(){
        System.out.println("Handle Button Called");

        Stage search = new Stage();
        FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Searcher.fxml"));
        search.setTitle("Searcher");
        try {
            AnchorPane searcherPage = (AnchorPane) loader.load();
            Scene scene = new Scene(searcherPage);
            search.setScene(scene);
            search.show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Here how do I load Searcher in BaseLayout's Rightside of split pane. Hope my question is clear. Thanks for your help in anticipation.

1
1
6/5/2014 9:01:35 AM

You can take pane on right side and then load fxml as child node of that pane. Stage is same for all time and you can load fxml in pane. ex: MainWindow.fxml is perent window. You can load in stage.

Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

Now in MainWindow.fxml you have split pane. Put anchorPan on right side where you want to load fxml on button click. Then add fxml as child in anchorpane.

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
Pane cmdPane = (Pane) fxmlLoader.load();
try {
anchCmdController.getChildren().clear();
anchCmdController.getChildren().add(cmdPane);
fadeIn.playFromStart();
} catch (Exception e) {
e.printStackTrace();
}
4
6/5/2014 9:54:19 AM

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