Load FXML Files in Anchorpane

53 views Asked by At

I Have a Main Anchorpane. Inside this anchorpane I have a Splitpane and inside this splitpane there is two Anchorpane(masterAnchorpane and sidebarAnchorpane). So I want to load rest of my FXML files in masterAnchorpane IMAGE ATTACHED FOR REFERENCE. I tried so many solutions in this case, but I can't solve this issue.

This is my code. Also I try many of codes to solve this.

FXMLLoader loader = new FXMLLoader(getClass().getResource("/FXML/CRM/Customer/customerMaster.fxml"));
                anchrMaster = loader.load();
                CustomerMasterController cmc = loader.getController();
                scene_customer = new Scene(anchrMaster);
                stage_customer.setScene(scene_customer);
                stage_customer.setResizable(false);
                cmc.setStage(stage_customer);
                stage_customer.show();

Error Shows in this code

class javafx.scene.control.SplitPane cannot be cast to class 
javafx.scene.layout.AnchorPane (javafx.scene.control.SplitPane is in module 
javafx.controls@20 of loader 'app'; javafx.scene.layout.AnchorPane is in 
module javafx.graphics@20 of loader 'app')

So please help to solve this issue. I'm a newbie in java and also there is limited references for Javafx20. Thanks for Advance

1

There are 1 answers

5
David Weber On BEST ANSWER

Preface:

Your code/stacktrace looks like anchrMaster is an AnchorPane and you are loading a SplitPane into it.

The method loader.load() loads the fxml file and returns a node of the root tag of the fxml file.

I suggest, that your fxml file has a SplitPane as root node.

Solution:

Load your fxml file into a parent node object and replace the content of your AnchorPane with it.

Parent root = loader.load();
anchrMaster.getChildren().clear();
anchrMaster.getChildren().add(root);