用于多个FXML的Java FX 1控制器-使用单例

用于多个FXML的Java FX 1控制器-使用单例,java,controller,fxml,fxmlloader,Java,Controller,Fxml,Fxmlloader,我目前正在从事一个JavaFX项目,我使用一个控制器来处理多个FXML(请不要问为什么)! 我面临的问题是,每次新的FXML调用,控制器都会创建一个新对象。 我希望始终使用同一个控制器,并尝试将控制器的构造函数实现为Singleton public Controller(){} private static Controller controller = null; public synchronized static Controller getInstance() { if (co

我目前正在从事一个JavaFX项目,我使用一个控制器来处理多个FXML(请不要问为什么)! 我面临的问题是,每次新的FXML调用,控制器都会创建一个新对象。 我希望始终使用同一个控制器,并尝试将控制器的构造函数实现为Singleton

public Controller(){}
private static Controller controller = null;

public synchronized static Controller getInstance() {
    if (controller == null)
        controller = new Controller();
    return controller;
}
这个策略给了我以下错误,因为如果不为每个FXML创建一个新的构造函数,JavaFX似乎无法工作。 还有其他可能的方法吗

这就是我面临的错误: /Users/dwome/git/4winner/4win/target/classes/win/javafxsecene.fxml:11

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:934)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at win.Main.start(Main.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil can not access a member of class win.Controller with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.Class.newInstance(Class.java:436)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 19 more

运行应用程序win.Main时出现异常

使用FXMLLoader加载fxml。您可以通过调用注入控制器


或者,在加载fxmls时使用。控制器工厂可以返回相同的控制器对象

我知道这个问题很老了,但由于您没有发表评论,您使它工作了,而且这个问题似乎被浏览了很多次,我将讲述我在面对相同问题时所做的事

我用达斯忍者在回答时说的话

我的错误并不完全相同,但我认为它无论如何都可以工作,我有一个控制器用于多个FXML

以下是我的步骤:

  • 从每个FXML中删除了
    fx:controller=“path.to.controller”
  • 我创建了一个函数
    loadFXML

    private void loadFXML(String pathToFile){
        try{
            InputStream fxmlStream = getInstance().getClass().getResourceAsStream(path);
            FXMLLoader loader = new FXMLLoader();
            loader.setBuilderFactory(new JavaFXBuilderFactory());
            loader.setLocation(getInstance().getClass().getResource(path));
            loader.setController(instance);
            BorderPane panee = (BorderPane) loader.load(fxmlStream);
            Scene scene = new Scene(pane);
        }
    }
    
  • 如您所见,我正在执行
    getInstance()
    loader.setController(instance)

  • 实例化
    实例
    。。。我是用“棘轮”的方式做的


  • 可能对你的情况没有帮助,因为这已经是6个月前的事了,但可能会帮助其他人

    你有这个好方法的例子吗?这个例子会是一个救命稻草!非常感谢你的努力!
    private static Controller instance;
    static{
         if (instance == null)}
              instance = new Controller();
         }
    }