在我的GUI应用程序中,我有两个视图:playlistView.fxml和videoView.fxml。每个都有自己的控制器。我想playListView是videoView布局的一部分,所以我用:JavaFX8 FXML控制器注入
<fx:include fx:id="idPlayListAnchorPane" source="playListView.fxml" />
包含的文件。工作正常,播放列表显示为videoView布局的一部分。
然后我注入idPlayListAnchorPane FXML可变进VideoViewController这样的:
@FXML
private AnchorPane idPlayListAnchorPane;
工作过。例如,我可以在playListView从VideoViewController与禁用idPlayListAnchorPane:
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中。几个小时后的简单问题是:为什么不呢?从<fx:include>
一次,一次,当你在代码中创建FXMLLoader
并调用load()
:
经过一番调查后,我发现ListView中的onAction处理程序不再触发,所以显然在listview控制器和videoViewController之间没有“连接”。我认为我最好的选择是fx:include,但忘记了一个单独的控制器并将代码粘贴到第一个控制器中。 – Frank
不要这样做。你仍然会有两个控制器,他们只会来自同一个班级。除了仍然存在如何在两个控制器实例之间进行通信的问题之外,要跟踪每个实例中哪些字段已初始化将非常困难。 (其中一个将从一个FXML初始化,另一个来自另一个FXML)。只需使用[documentation](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml)中显示的标准机制/doc-files/introduction_to_fxml.html#nested_controllers) –