Loki
Loki

Reputation: 4120

JavaFX Object is always null after launch

I'm trying to create an Application, using JavaFX. But the JavaFX Application is not the Main entrance of the Application.

I'm using a Main-Class, a Controller-Class (which controlls everything), and other classes like the JavaFX Application

Main -(calls)-> Controller -(creates)-> JavaFX Application

After the JavaFX Application Object is created, the Controller calls a method, so the JavaFX Application Object has an instance of Controller

But this object is always null, as soon as I'm outside of the method-call.

Main

public class Main{
    public static void main(String[] args){
        Controller c = new Controller();
    }
}

Controller

public class Controller{
    private MyApplication app;

    public Controller(){

        app = new MyApplication(); //create Application
        app.setController(this); //set Controller Object
        app.startApplication(); //launch the application

    }

}

MyApplication

public class MyApplication extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;
    private Controller controller;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        initRootLayout();
    }


    public void setController(Controller con){
        this.controller = con;
    }



    public void startApplication(String... args){
        launch(args);
    }

    public void initRootLayout(){
        System.out.println(controller==null); //returns true. But why?

        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MyApplication.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);   

            primaryStage.show();
        }
        catch(IOException e){
            e.printStackTrace();
        }

    }
}

How come, the Controller Instance within MyApplication is always null. The only time it's not null is within the call setController

Upvotes: 0

Views: 1140

Answers (2)

Nikolai97
Nikolai97

Reputation: 212

I think your problem is that you are referencing different instances of your Controller class. After the call to launch(args) JavaFX creates its own private instance of your Application class. When you call setController you are setting the controller for a different instance of your class than what JavaFX is using. One way to fix this would be to make the controller variable and its setter static.

Upvotes: 0

James_D
James_D

Reputation: 209225

Since JavaFX doesn't have system tray access, you basically need an AWT application to run in the system tray. I would then consider embedding the JavaFX aspects in Swing using a JFXPanel, and basically make it a Swing/AWT application with some JavaFX embedded.

Alternatively, you could launch everything from an Application subclass, and just bootstrap the AWT part in the start method, setting up the triggers to do the JavaFX stuff when needed. That feels uglier though.

Finally, your approach only really fails because you need to pass an object to the JavaFX application. If it makes sense to make that object a singleton, then you could just let the JavaFX application retrieve it, rather than passing the object to it.

Upvotes: 1

Related Questions