0
public static void callNewScene(String sceneJump, Event event) throws IOException {

    ((Node)event.getSource()).getScene().getWindow().hide();

    Stage primaryStage = new Stage();
    FXMLLoader loader = new FXMLLoader();
    Pane root = loader.load(getClass().getResource(sceneJump).openStream());
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();

}

I have this method that will hide current scene and show a new scene afterwards. But its giving me an error on this "getClass()." It doesn't seem recognize it. How can i fix this,? Please help. It is working if i put this directly on my controller but i dont want my code to be redundant.

Picture Example:

Or if you guys a better way Where i can make hide/open a new scene as I've said above please feel free to suggest thank you but its important to be method that pass a value.

Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
  • 1
    `Pane root = loader.load(YourClassName.class.getResource(sceneJump).openStream());` change `YourClassName` with the name of your class which `callNewScene` static method resides in. – guleryuz Apr 26 '18 at 05:05
  • 2
    It's better to use the url itself instead of using the stream. If you use the stream relative locations cannot be resolved properly... (remove the `.openStream()` in addition to using the fix described in the linked question.) – fabian Apr 26 '18 at 06:40
  • Could you show me an example of that @fabian – i4mB4rney 02 Apr 27 '18 at 02:29
  • I change my code to something like this as you've mention Pane root = loader.load(NewScene.class.getResource(sceneJump)); but this giving me an error says location is required. – i4mB4rney 02 Apr 27 '18 at 02:44
  • @i4mB4rney02 In that case `sceneJump` does not refer to a resoure location available via classpath. This wouldn't work with `.openStream()` either, since you'd get a `NullPointerException`. – fabian Apr 27 '18 at 05:55
  • Any idea how can i fix this? I'm pretty sure that `code`(sceneJump) has its proper value I am really having trouble how can I open a new scene. pls help – i4mB4rney 02 Apr 27 '18 at 13:55
  • I put "sample/".fxml", and it work thank you mate – i4mB4rney 02 Apr 27 '18 at 15:57

0 Answers0