我需要从javafx控制器
打开一个fxml窗口Stage graphStage = new Stage();
FXMLLoader loader = new FXMLLoader();
fenetre = new File("../graph/graph.fxml");
if (!fenetre.exists()) {
System.out.println("fichier inexistant");
} else {
Pane rootGraph = loader.load(getClass().getResource(fenetre.getPath()).openStream());
GraphController controller = (GraphController) loader.getController();
controller.getSystemChoice(id);
Scene sceneGraph = new Scene(rootGraph);
sceneGraph.getStylesheets().add(getClass().getResource("myStyle.css").toExternalForm());
graphStage.setTitle("Graphe");
graphStage.setScene(sceneGraph);
graphStage.show();
}
代码没有查找文件,如何访问此文件?
controler is on: cloud/composant/controler.java
my fxml is on: cloud/graph/graph.fxml
答案 0 :(得分:1)
您可以从文件中获取数据,也可以使用资源。
不要尝试将两种方法结合起来。它们通常是不相容的。
// assuming here the file path is correct
FXMLLoader loader = new FXMLLoader(fenetre.toURI().toURL());
...
Pane rootGraph = loader.load();
如果文件是资源,您应该更喜欢将其作为资源加载:
// assuming cloud is positioned in the "classpath root"
FXMLLoader loader = new FXMLLoader(getClass().getResource("/cloud/graph/graph.fxml"));