Exception has occured:Root value already specified in Javafx when loading fxml page for the second time


Question

public void loginFired(ActionEvent event) {
        try{
        Stage login = new Stage();
        loadView(new Locale("en", "EN"));
        URL location = getClass().getResource("login.fxml");
        fxmlLoader.setLocation(location);
        AnchorPane page = (AnchorPane) fxmlLoader.load(location.openStream());
       Scene scene = new Scene(page);
            hostLogin.setScene(scene);
            hostLogin.initStyle(StageStyle.UTILITY);
            hostLogin.setTitle("Login info");
            hostLogin.setResizable(false);
            hostLogin.initOwner(this.stage);
            hostLogin.initModality(Modality.APPLICATION_MODAL);
            hostLogin.show();

Here am using a loadview method behind the code to get load the properties file. For the first the page the event is firing properly, when i close the window and clicking on that button/loginFired again it is throwing an exception. Exception has occured:Root value already specified . Please guide me for the same. I am totally new to this javafx thing.!

1
2
2/19/2016 2:37:18 PM

Accepted Answer

The FXMLLoader is not yet meant to load an FXML file twice. You either have to

  • make a new FXMLLoader instance, set the location and load it
  • use the static load method
  • reset the current FXMLLoader instance by calling setRoot(null) (not recommended)
4
1/29/2014 8:12:33 AM

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