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 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".