17

How can I get the application instance when using javafx?

Normally you launch an application this way:

public class LoginForm {

    public static void main(String[] args) {
        LoginApplication.launch(LoginApplication.class, args);
    }

}

The method launch does not return an instance of application. Is there a way I could get the instance?

user2997204
  • 1,344
  • 2
  • 12
  • 24
  • Did you google it ? http://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html – Dici Dec 26 '14 at 03:31
  • @Dici What do you mean? – user2997204 Dec 26 '14 at 03:33
  • Everything you need is in the doc. Your main class just need to extend `Application` and then your main method will just call the `launch` method. Actually, a main method isn't even necessary to run a JavaFX application – Dici Dec 26 '14 at 03:35
  • 1
    @Dici Why couldn't they just make it return an instance.. – user2997204 Dec 26 '14 at 03:38
  • Why should they ? I find it convenient enough this way. Same thing exists in Scala in a more general way with the `App` class – Dici Dec 26 '14 at 03:41
  • 2
    IIRC Launch only returns when the app is shutdown so returning a new instance won't help you anyways – tomsontom Dec 26 '14 at 08:19
  • I found this trick, it still doesn't answer the question, but may help to find a workaround:http://stackoverflow.com/questions/13328398/using-a-javafx-application-instance-from-another-class – pdem Jul 27 '16 at 15:11

1 Answers1

10

I was just trying to find an easy, logical way to do exactly this. I haven't. It would be really nice if there was an Application.getApplicationFor(AppClass.class) that managed some singletons for you--but no.

If we restrict the problem space it's pretty easy to solve. If we make the class a singleton, then it's cake... A simplified singleton pattern should work fine:

class MyApp extends Application
{           
    public static MyApp me;
    public MyApp()
    {
        me=this;
    }

    ... 
}

me can be null if it hasn't been instantiated by the system yet. It would be possible to protect against that with more code.

... implementing code...

Just implemented this--seems to work (barring any strange threading situations) I have a slightly different situation, I'm wedging a javaFX screen into an existing swing GUI. It works fine, but I need to ensure that Application.launch is only called once. Adding this requirement, my final solution is thus:

(Sorry but the syntax has some groovy in it, should be easy for any Java user to translate)

class MyClass extends Application{
    private static MyClass instance

    public MyClass() {
        instance=this
    }

    public synchronized static getInstance() {
        if(!instance) {
            Thread.start { 
            // Have to run in a thread because launch doesn't return
            Application.launch(MyClass.class)
        }
        while(!instance)
            Thread.sleep(100)
    }
    return instance
  ...
} // class

This manages blocking until Application.launch has completed instantiating the class, getting instances from other locations and ensuring that Application.launch gets called exactly once as long as getInstance has been called at least once.

Bill K
  • 62,186
  • 18
  • 105
  • 157
  • I'd normally protest against the use of singleton, but extraneous circumstances aside, for a simple client application, this should work fine, at least until more complicated solutions are required. – Neil Sep 18 '17 at 13:59
  • Instead of looping and polling, I'd suggest a countdown latch. If you don't actually need to specifically *return* the instance but just to act upon it, then better still would be to set a consumer on the static field and have the no-arg constructor call the consumer. Then you don't have to wait on anything. – drrob Jul 25 '18 at 16:36