How to create a splash screen as a preloader in a stand-alone JavaFX application?

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

9.3.4 Using the preloader to display application initialization results 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 would like to run preloader every time I launch 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 launch the main application, the preloader does not start, so I assume that I have something missing. I am new to Preloaders and I don’t understand what is the mechanism for connecting the main application with the preloader in a standalone application.

+8
javafx splash-screen preloader
source share
3 answers

You can use LauncherImpl like this.,.

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

And the MyApplication class will be like that.,.

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

IDEs are not very good at adding preloaders. Take a look at the manifest in the jar program file and make sure this line is present:

 JavaFX-Preloader-Class: SplashScreenLoader 
+2
source share

Maybe too late, it can help someone too. For me, I used the JavaFX service and the task to create a splash screen as a preloader in a stand-alone JavaFX application. This is because the context of my project.

Create Anchor Panel and Progress Panel

 @FXML private AnchorPane anchorPane; private MaskerPane progressPane; public static void main(String[] args) { launch(args); } @Override public void init() throws Exception { progressPane = new MaskerPane(); progressPane.setText(bundle.getString("root.pleaseWait")); progressPane.setVisible(false); AnchorPane.setLeftAnchor(progressPane, 0.0); AnchorPane.setTopAnchor(progressPane, 0.0); AnchorPane.setRightAnchor(progressPane, 0.0); AnchorPane.setBottomAnchor(progressPane, 0.0); anchorPane.getChildren().add(progressPane); } @Override public void start(Stage initStage) { //..... initRoot(); //..... } 

Create a screensaver service like this:

 private final Service<Void> splashService = new Service<Void>() { @Override protected Task<Void> createTask() { return new Task<Void>() { @Override public Void call() throws Exception { //main code, the code who take time //or //Thread.sleep(10000); return null; } }; } }; 

Start the service and show / hide the progress panel on initRoot when loading the main screen:

  public void initRoot() { try { //.... splashService.restart(); //On succeeded, do this splashService.setOnRunning(new EventHandler<WorkerStateEvent>() { @Override public void handle(WorkerStateEvent event) { //Show mask on succeed showMask(Boolean.TRUE); } }); splashService.setOnSucceeded(new EventHandler<WorkerStateEvent>() { @Override public void handle(WorkerStateEvent event) { splashService.cancel(); //Hide mask on succeed showMask(Boolean.FALSE); } }); //..... primaryStage.show(); } catch (IOException ex) { ex.printStackTrace(); } } 

To show / hide progress ...

 showMask(boolean value){ progressPane.setVisible(value); }; 
0
source share

All Articles