0

I have an annoying problem in my JavaFX project : I want to open an other fxml file (in a new window) on a button click in my main window. Here is my code below :

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.net.URL;

public class Controller {

@FXML
public void newButton(ActionEvent event) throws Exception {
    try {
        URL fxmlLocation = getClass().getResource("/resources/new_wind.fxml");
        System.out.println(fxmlLocation);

        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("/resources/new_wind.fxml"));

        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setTitle("windows");
        stage.setScene(new Scene(root1));
        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

}

My file structure : enter image description here

My main window is in sample.fxml, and the window I want to open is in resources folder (marked as resources folder in intellij) : "window.fxml".

When I try to click on this button, I got this error which refers to a path problem : "java.lang.IllegalStateException: Location is not set."

The line "System.out.println(fxmlLocation)" returns a null value indeed, so how to get the "correct" path for my new file ?

I precise that I got also an unknow class error in my window.fxml file, caused by this line : "fx:controller="Window".

UVision
  • 11
  • 4
  • Does this answer your question? [JavaFX java.lang.IllegalStateException: Location is not set](https://stackoverflow.com/questions/43721851/javafx-java-lang-illegalstateexception-location-is-not-set) – Alex May 14 '21 at 09:35
  • Have you try without "/resources", It is marked as resources root and I guess should not be part of the final path – Bastien Aracil May 14 '21 at 09:48

0 Answers0