在我的GUI应用程序中,我有两个视图:playlistView.fxml和videoView.fxml.每个人都有自己的控制器。我希望playListView成为videoView布局的一部分,因此我使用:
<fx:include fx:id="idPlayListAnchorPane" source="playListView.fxml" />
要包含该文件。工作正常,播放列表显示为videoView布局的一部分。
然后,我将idPlayListAnchorPane FXML变量注入VideoViewController,如下所示:
@FXML
private AnchorPane idPlayListAnchorPane;
也很管用。例如,我可以通过以下方式从idPlayListAnchorPane中禁用playListView中的VideoViewController:
idPlayListAnchorPane.setDisable(true);
要获得我使用的playListViewController:
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/designer/views/video/playListView.fxml"));
PlayListViewController playListViewController = new PlayListViewController();
loader.setController(playListViewController);
try {
AnchorPane playListView = (AnchorPane) loader.load();
} catch (IOException e) {
};
然后我可以打电话给你,例如:
playListViewController.init();
来自videoViewController.
但是init()方法在playListView ListView中创建了一些测试值(测试是作为一个单独的应用程序进行的)。但是,这些测试值现在不会出现在ListView中。经过许多小时后的简单问题是:为什么不呢?
发布于 2015-09-05 18:51:50
您将加载playListView.fxml
文件两次:一次是从<fx:include>
加载,一次是在代码中创建FXMLLoader
并调用load()
时加载。AnchorPane
创建的节点层次结构(即<fx:include>
及其所有内容)显示在GUI中,而FXMLLoader.load()
调用创建的节点层次结构则不显示。
由于您创建的控制器与未显示的节点层次结构相关联,因此您在控制器上调用的方法将不会对您的UI产生任何影响。
与创建FXMLLoader
来获取控制器实例不同,您可以使用文档中描述的嵌套控制器技术将包含的FXML中的控制器直接注入到您的VideoViewController
中。
为此,首先向您的fx:controller
根元素添加一个playListView.fxml
属性:
playListView.fxml:
<!-- imports etc -->
<AnchorPane fx:controller="com.mycompany.packagename.PlayListViewController">
<!-- etc etc -->
</AnchorPane>
由于在您的fx:id="idPlayListAnchorPane"
上定义了一个<fx:include ...>
属性,所以可以通过使用名为idPlayListAnchorPaneController
的@FXML
-annotated字段将控制器直接注入到VideoViewController
类中(规则是将" controller“附加到id中):
public class VideoViewController {
@FXML
private AnchorPane idPlayListAnchorPane;
@FXML
private PlayListViewController idPlayListAnchorPaneController ;
// ...
}
现在,您可以根据需要调用控制器上的方法。
https://stackoverflow.com/questions/32407666
复制相似问题