0

GUI example

to create a new release the user would be prompted to click on the plus button in the bottom left of the GUI, when they click on this I want a pane overlayed on top of the center of the GUI allowing users to enter settings and specify options for a new release, I've tried this:

 private void addRelease(Event event) throws IOException {
     Popup popup = new Popup();
    NewReleasePopupController controller = new NewReleasePopupController();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/NewReleasePopup.fxml"));
    loader.setController(controller);
    loader.load();
    popup.getContent().add((Parent)loader.load());
}

however it seems to throw errors while loading, I would like to avoid loading a separate stage if at all possible and would like to have the controller for the popup to be nested within the controller for the main stage. I tried using the popup class if anyone can help me to get that working or has any better ways to achieve this help would be much appreciated !!

the error:

Caused by: javafx.fxml.LoadException: Controller value already specified.

/D:/DropDayAIO/out/production/DropDayAIO/DropDayAIO/Resources/newReleasePopup.fxml:6

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at DropDayAIO.HomeSceneController.addRelease(HomeSceneController.java:151)
at DropDayAIO.HomeSceneController.addNewRelease(HomeSceneController.java:128)
... 57 more
  • If it `throws errors` why not include the error, so we can take a look at it? – n247s Feb 05 '20 at 18:45
  • @n247s I have edited it for you –  Feb 05 '20 at 19:04
  • Maybe this will help? [JavaFX FXML controller - constructor vs initialize method](https://stackoverflow.com/questions/34785417/javafx-fxml-controller-constructor-vs-initialize-method) – Abra Feb 05 '20 at 19:16
  • You could look into [`ControlsFX` `Dialogs`](http://fxexperience.com/controlsfx/features/dialogs/) or [`JavaFX` `Alerts`](https://code.makery.ch/blog/javafx-dialogs-official/). – SedJ601 Feb 05 '20 at 20:36

1 Answers1

1

Based on the error you mentioned, I think you already have a controller declaration in the FXML file. like with attribute.. fx:controller

Usually while having this attribute declaration in fxml , if you try to set controller again, it throws Caused by: javafx.fxml.LoadException: Controller value already specified

And I believe this should be the case, because you can notice the error is raised at processAttribute method.

javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)

You can either remove it from the fxml or dont need to set in the code (through setController()). If you say, you want the reference of the controller, you can always get through loader.getController().

And top of that, if you manage to resolve Controller value already specified error, I think next you will encounter Caused by: javafx.fxml.LoadException: Root value already specified.Because you are trying to call the load() method of the FXMLLoader two times. Assign the node to a variable and use that variable to set in popup.

I believe the below code should solve the issue. Give a try!!

private void addRelease(Event event) throws IOException {
     Popup popup = new Popup();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/newReleasePopup.fxml"));
    Parent parent = (Parent)loader.load(); // I am not sure you need a cast here
    newReleasePopupController controller = loader.getController();
    popup.getContent().add(parent);
}

Suggestion: Please use proper naming conventions of class.

Sai Dandem
  • 8,229
  • 11
  • 26
  • "_// I am not sure you need a cast here_" – the cast is unnecessary, as the return type of `#load()` is `T` which will be inferred (to `Parent`) from the left-hand side of the assignment in this case. – Slaw Feb 06 '20 at 17:26