Search code examples
javafxsplash-screenpreloader

How to create splash screen as a Preloader in JavaFX standalone application?


I created a Preloader (based on the following tutorial) that should display a splash screen for the main application.

9.3.4 Using a Preloader to Display the Application Initialization Progress http://docs.oracle.com/javafx/2/deployment/preloaders.htm

public class SplashScreenLoader extends Preloader {

    private Stage splashScreen;

    @Override
    public void start(Stage stage) throws Exception {
        splashScreen = stage;
        splashScreen.setScene(createScene());
        splashScreen.show();
    }

    public Scene createScene() {
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 200);
        return scene;
    }

    @Override
    public void handleApplicationNotification(PreloaderNotification notification) {
        if (notification instanceof StateChangeNotification) {
            splashScreen.hide();
        }
    }

}

I'd like to run preloader each time I run the main application in my IDE (IntelliJ IDEA).

I also followed the packaging rules for preloaders in IntelliJ: https://www.jetbrains.com/idea/help/applications-with-a-preloader-project-organization-and-packaging.html

When I run the main application the preloader doesn't start, so I suppose I'm missing something. I'm new to Preloaders and I don't understand what is the mechanism to connect the main app with the preloader in standalone application.


Solution

  • You can run using LauncherImpl like this . . .

    public class Main {
       public static void main(String[] args) {
          LauncherImpl.launchApplication(MyApplication.class, SplashScreenLoader.class, args);
       }
    }
    

    And the class MyApplication would be like this . . .

    public class MyApplication extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            ....
            primaryStage.show();
        }
        public static void main(String[] args) {
            launch(args);
        }
    }