Create a JavaFX primary stage inside a normal Java application

swaechter picture swaechter · Feb 11, 2013 · Viewed 8.6k times · Source

I have a launcher and a JavaFX class. The launcher creates a class called JavaFXApplication1. The JavaFXApplication contains the whole JavaFX code (Just a little example in this case) and should setup a window with one primary stage.

The launcher has the static main entry point - but I read that JavaFX doesn't really use this entry point. This explains my console output (See the end of the post)

I don't know if this is possible (Launcher create a JavaFX window - the entry point is not in the presentation class itself) . I don't want to use a preloader (I think preloaders are just for heavy loads during startup), because the launcher represents the whole program as one object (Presentation, business and persistence - a 3 layer program). The entry point should be outside the presentation class (in this example in the launcher class)

The following example does work. But for me it is like a piece of "black magic"

Here is my code

Launcher:

package javafxapplication1;

public class Launcher
{
    public static void main(String[] args)
    {
        System.out.println("main()");

        // Do some stuff and then create the UI class

        JavaFXApplication1 client = new JavaFXApplication1();
        client.caller(args);
    }
}

JavaFXApplication1:

package javafxapplication1;

import javafx.application.Application;
import javafx.stage.Stage;

public class JavaFXApplication1 extends Application
{ 
    @Override
    public void start(Stage primaryStage)
    {
        System.out.println("start()");
        primaryStage.setTitle("I am a JavaFX app");
        primaryStage.show();
    }

    public void caller(String[] args)
    {
        System.out.println("caller()");
        launch(args);
    }
    /* We call the main function from the client
    public static void main(String[] args)
    {
        launch(args);
    }*/
}

And the output for the program is:

start()

Is there a way to create such an application ? Thank you

Answer

swaechter picture swaechter · Feb 13, 2013

The answer to this problem is to create a java project and not a JavaFX project. After this you can add a JavaFX main class and write a method (call launch() ).

Maybe you have to add the compile-time libraries deploy.jar, javaws.jar, jfxrt.jar and plugin.jar from the /jdk_*/jre/lib directory