JAVAFX - FXML - 从父控制器访问加载的FXML控件 [英] JAVAFX - FXML - Access Loaded FXML Controls from Parent Controller
问题描述
以下是我要完成的工作。
Here is what I am trying to accomplish.
/Package A/
/Package A/ApplicationController.java
/Package A/Application.fxml
在我的Application.fxml文件中,我有一个按钮,当单击该按钮时,它会加载以下MyGrid.fxml文件。
In my Application.fxml file I have a button, and when that button is clicked it loads the following "MyGrid.fxml" file.
/Package B/
/Package B/MyGrid.fxml (has a label #mygridlabelid
我使用的代码是:
ContentPane.getChildren().add((Node)FXMLLoader.load(getClass().getResource("/Package B/MyGrid.fxml")));
但问题是..即使我正在加载MyGrid来自ApplicationController的.fxml文件,我无法从ApplicationController文件访问#mygridlabelid。我在ApplicationController.java文件中定义了@FXML标签mygridlabelid,但它没有实例化:(
But the problem is.. even though I am loading the MyGrid.fxml file from the ApplicationController, I cannot access #mygridlabelid from the ApplicationController file. I defined @FXML label mygridlabelid in the ApplicationController.java file, but it doesn't get instantiated :(
我该怎么办呢?周围有什么技巧或黑客?
How can I do that? Any tricks or hacks around it?
我设法通过执行以下操作来解决问题...并确保.fxml文件没有fx:controller set。否则你将遇到已指定控制器值。
I managed to solve the problem by doing the following... and make sure that the .fxml file does not have fx:controller set. Or else you will run into "Controller value already specified."
FXMLLoader loader = new FXMLLoader(getClass().getResource("/your.fxml"));
loader.setController(this);
try {
ContentPane.getChildren().add((Node)loader.load());
} catch (IOException e){
System.out.println(e.getMessage());
}
这篇关于JAVAFX - FXML - 从父控制器访问加载的FXML控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!