Changing Scene when a button is pressed Fxml
Solution 1
It's on the classpath so it should be the resource should be precedded with a forward slash i.e. /second.fxml
That is assuming it is not in a package. Make sure your build system is also copying it to the output directory along with your class files.
Solution 2
"Ah, then you need /demo/second.fxml as the resource path. Saying that, I am confused why the error message says sucess.fxml and not second.fxml. If the above suggestion doesn't work can you post the fxml? – Andy Till"
This comment from Andy Till is the answer to the question. Thanks Andy. You solved my problem :) Thumbs up !!!
Include the package name. For ex:
/package_name/fxml_file_name.fxml
Vinod CG
UX Developer, Web application developer and Game developer
Updated on June 14, 2022Comments
-
Vinod CG almost 2 years
I have 2 fxml files mainFxml.fxml and second.fxml. Main fxml has a button with fx:id="change". I want change scene when change button is clicked. Code of mainFxml controller
public void onChangeButtonAction(event e){ Node node=(Node) event.getSource(); Stage stage=(Stage) node.getScene().getWindow(); Parent root = FXMLLoader.load(getClass().getResource("second.fxml"));/* Exception */ Scene scene = new Scene(root); stage.setScene(scene); stage.show(); }
but when i press button change it throws exception as no resource specified. Help me..
Stack trace
No resources specified. file:/E:/Projects/javaFx/demo/dist/demo.jar!/demo/sucess.fxml:14 at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:305) at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:197) at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:588) at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683) at demo.myFirstFxmlController.onSuccess(myFirstFxmlController.java:130) at demo.myFirstFxmlController.OnSubmitAction(myFirstFxmlController.java:53)
-
Vinod CG over 11 yearsall the file are in the same directory
-
Sergey Grinev over 11 yearsyep, second one is a common issue. If you are in Netbeans run Clean and Build to ensure that.
-
Vinod CG over 11 yearsIts being included in the package.. I checcked using winrar.. Error exists even after changing it to /second.fxml
-
Andy Till over 11 yearsIs it in the root of jar is it it another directory?
-
Vinod CG over 11 yearsThere is a package with name demo and all the files are inside it
-
Andy Till over 11 yearsAh, then you need /demo/second.fxml as the resource path. Saying that, I am confused why the error message says sucess.fxml and not second.fxml. If the above suggestion doesn't work can you post the fxml?