Switch between fxml files

纵饮孤独 提交于 2019-12-02 09:38:57

You should never instantiate the controller with new when you are using FXML because the job to instantiate the controller is done by the FXMLLoader. While instantiating the controller, it also creates instances of the nodes which are there in the FXML and inject them into the controller.

If you do not get the controller instance from the FMXLLoader, all your nodes inside the controller which are annotated with @FXML eill be null. Therefore, you must always get the Controller out of the fxml.

In your case, you should use

bpic = fxmlLoader.getController();

instead of

bpic = new BottomPanelIncomingController();

Update

To change the FXML on click of a button

Let us consider the following method is called on Button click

@FXML
public void callAccepted(ActionEvent event){
    System.out.println("From controller");
    nrb.loadSecondFxml();
}

You can load the FXML and set it on a Scene and then to the JFXPanel

public void loadSecondFxml(){
    //Load new FXML and assign it to scene
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("newFXML.fxml"));
    Parent root = (Parent) fxmlLoader.load();
    Scene scene = new Scene(root, 600, 65);
    jfxPanel.setScene(scene);
} 

Note : I am not sure what you are trying to achieve here, consider this as an example just to load FXML on click of a button and apply your logic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!