I have problem when trying to close current scene and open up another scene when menuItem is selected. My main stage is coded as below:
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("cartHomePage.fxml"));
Pane myPane = (Pane) myLoader.load();
CartHomePageUI controller = (CartHomePageUI) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
When the program is executed, it will go to the cartHomePage.fxml. From there, I can select to go to create product or create category when the menu item is selected. Here is my action event:
Stage prevStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createCategory.fxml"));
Pane myPane = (Pane) myLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
setPrevStage(stage);
stage.show();
}
//Method to change scene when menu item create product is on click
@FXML
public void gotoCreateProduct(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("creatProduct.fxml"));
Pane myPane = (Pane) myLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
setPrevStage(stage);
stage.show();
}
However, I can only switch the stage once. For example, my default page is cartHomePage.fxml. When I run the program, first I go to create product stage. After that, I cannot go to anywhere any more. The error message is:
java.lang.IllegalStateException: Location is not set.
and Null Pointer Exception
I did set the stage after I close it and pass it around. I wonder which part went wrong.
Thanks in advance.
I converted a simple NetBeans 8 Java FXML application to the Maven-driven one. Then I got problems, because the
getResource()
methods weren't able to find the .fxml files. In mine original application the fxmls were scattered through the package tree - each beside its controller class file. After I made Clean and build in NetBeans, I checked the result .jar in the target folder - the .jar didn't contain any fxml at all. All the fxmls were strangely disappeared.Then I put all fxmls into the resources/fxml folder and set the getResource method parameters accordingly, for example:
FXMLLoader(App.class.getClassLoader().getResource("fxml/ObjOverview.fxml"));
In this case everything went OK. The fxml folder appeared int the .jar's root and it contained all my fxmls. The program was working as expected.The answer below by CsPeitch and others is on the right track. Just make sure that the fxml file is being copied over to your class output target, or the runtime will not see it. Check the generated class file directory and see if the fxml is there
in my case i just remove ..
I've stumbled upon the same problem. Program was running great from Eclipse via "Run" button, but NOT from runnable JAR which I'd exported before. My solution was:
1) Move Main class to default package
2) Set other path for Eclipse, and other while running from the JAR file (paste this into Main.java)
And after that in start method you have to load files like this:
It works for me in Eclipse Mars with e(fx)clipse plugin.
mine was strange... IntelliJ specific quirk.
I looked at my output classes and there was a folder:
x.y.z
instead of
x/y/z
but if you have certain options set in IntelliJ, in the navigator they will both look like x.y.z
so check your output folder if you're scratching your head
I tried a fast and simple thing:
I have two packages ->
app.gui
andapp.login
In my login class I use the
mainview.fxml
fromapp.gui
so I did this in thelogin.fxml
And it works :)