Vinod CG
Vinod CG

Reputation: 1061

Changing Scene when a button is pressed Fxml

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)

Upvotes: 2

Views: 12733

Answers (4)

James
James

Reputation: 1

import java.io.IOException;

I got it to work by either surrounding the load(getClass... with a try/catch block, or by just importing java.io.IOException.

Enjoy :)

Upvotes: 0

Raj Wadhwa
Raj Wadhwa

Reputation: 335

"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

Upvotes: 1

Andy Till
Andy Till

Reputation: 3511

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.

Upvotes: 1

Sergey Grinev
Sergey Grinev

Reputation: 34478

Most probably your file second.fxml is not located in the same folder with mainFxml.fxml's controler java file.

Upvotes: 0

Related Questions