java - 如何获取新阶段 Controller JavaFX 的句柄

标签 java javafx controller scene

我正在开始使用它自己的 Controller 的第二个场景。我想从另一个类访问该 Controller 中的方法。如何获取新场景 Controller 的句柄?

public void startNewScene() throws IOException{
     Stage stage = new Stage();
     Partent root;
     root = FXMLLoader.load(getClass().getResource("fxmlfile.fxml");
     Scene scene = new Scene(root);
     Stage.setScene(scene);
     stage.show();

}

最佳答案

创建一个 FXMLLoader 实例(而不是使用static load(...) 方法),并从中获取 Controller :

public void startNewScene() throws IOException{
    Stage stage = new Stage();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("fxmlfile.fxml"));
    Parent root = loader.load();
    MyController controller = loader.getController();
    Scene scene = new Scene(root);
    Stage.setScene(scene);
    stage.show();
}

显然将 MyController 替换为 fxmlfile.fxml Controller 的实际类名。

关于java - 如何获取新阶段 Controller JavaFX 的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40344351/

相关文章:

php - 如何压缩此代码以从 Laravel Controller 向 View 发送信息?

java - Swing JLabel 缺少字符

java - 将组件添加到 controlfx maskerpane

java - 如何在连接MySQL数据库之前制作加载场景?

java - 如何向按钮添加方法以在 JavaFX 中的场景之间切换

c++ - C++ 中的 Controller /迷你内核设计模式

c# - 当 RedirectToAction 调用 View 时,mvc4 模型为 null

java - Option[T] 类的意义何在?

java - 如何比较两个 XSD ComplexType 是否相等?

java - 从源代码编译 Jython 生成 org.python.parser 目录