简体   繁体   中英

Java FX 1 Controller for multiple FXML - Use Singleton

I am currently working on a Java FX project and I use one Controller for multiple FXML (Please don't ask why)! The problem I am facing is, that with every new FXML call, the controller creates a new object. I want to work with the same controller the whole time and tried to implement the Constructor of the Controller as Singleton

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

public synchronized static Controller getInstance() {
    if (controller == null)
        controller = new Controller();
    return controller;
}

This strategy gives me following error, as it seems that Java FX is not able to work without creating a new constructor for each FXML. Is there any other approach possible?

This is the error i am facing: /Users/dwome/git/4winner/4win/target/classes/win/javafxscene.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

Exception running application win.Main

I know the question is old, but since you haven't comment you made it work and this question seems to be viewed a lot, I'll say what I did when I faced the same problem.

I used what Darth Ninja said in his answer.

My error was not exactly the same, but I think it could work anyway, I had one controller for multiple FXML.

Here's my steps:

  1. Removed fx:controller="path.to.controller" from each FXML
  2. I created a function 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); } } 

As you see I'm doing getInstance() and loader.setController(instance);

  1. Instantiate instance ... I did it a "ratchet" way

     private static Controller instance; static{ if (instance == null)} instance = new Controller(); } } 

May not help your case, since it's 6 month ago, but may help others!

Use the FXMLLoader to load your fxml. You can call inject the controller via setController() .

Alternatively, use the Controller Factory when loading your fxmls. You controller factory can return the same controller object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM