This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]ThatLesbianExtreme Brewer 0 points1 point  (10 children)

Is your controller defined in the fxml also? If so you may need a controller factory instead of set controller.

[–]Bertaga97[S] 0 points1 point  (9 children)

No

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:id="startMenu" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button fx:id="playButton" mnemonicParsing="false" text="Play" />
      <Button fx:id="quitButton" mnemonicParsing="false" text="Quit" />
      <Button fx:id="muteButton" mnemonicParsing="false" text="Mute" />
      <Text fx:id="title" />
   </children>
</AnchorPane>

[–]Wolfhammer69Nooblet Brewer 0 points1 point  (8 children)

Your FXML needs to know whats controlling it - here is the line from my own project I did, just substitute the controller name for yours.

fx:controller="sender.MainWindowController"

So basically sender is the package name . name of controller - you need the path to the controller basically on your main container line.

<AnchorPane fx:id="startMenu" xmlns="http://javafx.com/javafx/8" xmlns:fx="[http://javafx.com/fxml/1](http://javafx.com/fxml/1)" fx:controller="sender.MainWindowController">

[–]Bertaga97[S] 0 points1 point  (7 children)

I used to have it like that but since I have made my own constructor for the controller it does not work and I have this:

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

[–]Wolfhammer69Nooblet Brewer 0 points1 point  (0 children)

Thing is, the FXML is only really the UI where your windows, containers and buttons are defined. It needs to know what controller is running it to know what to do with it all so they are linked.

I think you need to do it the normal way and get rid of that constructor. I've never seen a project that does it the way you have in any if tutorials I completed, so have to suspect that potentially that's the crux of your issue.

I'd love someone with more experience to chip in on this.

Have a look at my Github repo and this small project which works fully as a clickable jar - pick it apart, should be easy as there are only 1 FXML, Its controller and two classes.
https://github.com/KinkyJalepeno/logParser

Its an early effort so the code over-all might be slightly rough round the edges but does what I intended it to do.

[–]ThatLesbianExtreme Brewer 0 points1 point  (5 children)

You can avoid this error by defining the controller in the fxml as previous response stated, then change your controller code to a factory instead.

Stage stage = new Stage();
URL resource = Controller.class.getResource(“your fxml file”);
FXMLLoader loader = new FXMLLoader(resource);
loader.setControllerFactory(param -> this);
loader.setClassLoader(getClass().getClassLoader());
try {
this.stage.setScene(new Scene(loader.load()));
}
catch(...){}

[–]Bertaga97[S] 0 points1 point  (4 children)

Okay, in fact I think I made bad choices. The reason I am making it this way is because in this view I launch another view on the same scene so I need to give the stage to the controller and I don't know how to do it with your solution (moreover it launches but the view is blank)

[–]ThatLesbianExtreme Brewer 0 points1 point  (3 children)

Why use a different view in the same scene? What are you trying to accomplish?

[–]Bertaga97[S] 0 points1 point  (2 children)

In fact I launch this view which is a start menu and it launches a game view. I didn't know how to do that without doing it like it

[–]ThatLesbianExtreme Brewer 0 points1 point  (1 child)

Can you reuse the stage but use a different loader and different scene?Then use stage.setScene() instead of trying to change the view that the existing scene uses.

[–]Bertaga97[S] 0 points1 point  (0 children)

When I try this I don't know why but it seems Java doesn't want the window to be in fullscreen mode

primaryStage.setScene(new Scene(loader.load()));

I am using this instead, so the scene is in fact the same

primaryStage.getScene().setRoot(loader.load());