javafx - 在 Controller 中加载 FXML 文件,但也使用 NetBeans "Make Controller"

javafx - load FXML file inside Controller, but also use NetBeans's "Make Controller"

在尝试加载 FXML 文件时,通常会执行以下操作:

FXMLLoader loader = FXMLLoader.load(getClass().getResource("File.fxml"));
Region root = loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();

然而,当我试图将加载代码放入 "caller convenience" 的控制器时,我做了以下操作:

public Controller()
{
   FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
   loader.setController(this);
   Parent root = loader.load();
   Stage stage = new Stage();
   stage.setScene(new Scene(root));
   stage.show();
}

这工作得很好,因为现在我只需要调用构造函数来创建新的 window。

但我不得不删除

fx:controller="package.Class"
FXML 文件中的

属性,否则当我调用

时抛出异常 ("javafx.fxml.LoadException: controller is already set")
fxmlloader.setController(this);

构造函数中的方法。 由于我使用的是 NetBeans 及其 "Make Controller" 功能(右键单击 FXML 文件),因此无法创建控制器 class,因为缺少属性。

总结:

我想要实现的是一种在 FXML(对于 NetBeans)中仍然设置 "fx:controller" 属性并且能够在控制器 class 中方便地加载 FXML 的方法。

这可能吗,还是我需要某种包装器 class 来创建 FXML window(s)?

提前致谢。

你可以这样做:

public Controller()
{
   FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
   loader.setControllerFactory(type -> {
       if (type == Controller.class) {
           return this ;
       } else {
           try {
               return type.newInstance();
           } catch (RuntimeException e) {
               throw e ;
           } catch (Exception e) {
               throw new RuntimeException(e);
           }
       }
   });
   Parent root = loader.load();
   Stage stage = new Stage();
   stage.setScene(new Scene(root));
   stage.show();
}

这将允许您(事实上,您将需要)在 FXML 文件中具有 fx:controller 属性。基本上,它所做的是指定一个函数,FXMLLoader 可以使用该函数从指定的 class 中获取控制器实例。在这种情况下,如果 FXML Loader 正在寻找 Controller class 的对象,它会 returns 当前对象,否则只会创建指定 class 的新对象.