How to create a custom loading screen in JavaFX?
Asked Answered
S

5

9

I'd like to create a custom loading screen for a JavaFX application. Don't want the user to see the Java coffee cup icon, I want to put my own graphic there!

I've found out how to provide a static image, or even an animated GIF, but I'm more interested in a Flash-like screen where I can specify what the state of the image looks like at certain percentages.

Any ideas?

Scratchboard answered 11/12, 2008 at 22:24 Comment(2)
This should be easily doable if you are using java web-start to launch your application. In the JNLP file, you can mention: <icon href="splashIcon_64x64.png" kind="splash"/> <icon href="shortcutIcon_32x32.png" kind="default"/>. This is under the <information> tag.Meandrous
This question is old and the answers too, but I have provided a very simple workable example down below: https://mcmap.net/q/1018314/-how-to-create-a-custom-loading-screen-in-javafxCellophane
C
1

JavaFX preloader class

I have created a very simple preloader screen using native JavaFX APIs. Here it's explained how to do this: https://docs.oracle.com/javafx/2/deployment/preloaders.htm (old but workable examples) - this is newer and seems to be the same: https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/preloaders.html (Newer page and JavaFX version but I don't see the difference).

The older link is easier to read, because of page formatting.

Main class

package YOUR_PACKAGE_NAME;

import javafx.application.Application;

/**
 * Minimal reproducible example (MRE) - Example of a simple JavaFX preloader.
 * Java Main class for starting up the JavaFX application with a call to launch MainApplication.
 * @author Remzi Cavdar - [email protected] - <a href="https://github.com/Remzi1993">@Remzi1993</a>
 */
public class Main {
    public static void main(String[] args) {
        /*
         * The following Java system property is important for JavaFX to recognize your custom preloader class.
         * Which should extend javafx.application.Preloader.
         */
        System.setProperty("javafx.preloader", Preloader.class.getName());
        // Launch the main JavaFX application class.
        Application.launch(MainApplication.class, args);
    }
}

Preloader class

package YOUR_PACKAGE_NAME;

import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * Minimal reproducible example (MRE) - Example of a simple JavaFX preloader class.
 * @author Remzi Cavdar - [email protected] - <a href="https://github.com/Remzi1993">@Remzi1993</a>
 */
public class Preloader extends javafx.application.Preloader {
    private ProgressBar progressBar;
    private Stage stage;

    private Scene createPreloaderScene() {
        progressBar = new ProgressBar();
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(progressBar);
        return new Scene(borderPane, 800, 600);
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        // I also recommend to set app icon: stage.getIcons().add();
        stage.setTitle("YOUR TILE HERE");
        stage.setScene(createPreloaderScene());
        stage.show();
    }

    @Override
    public void handleProgressNotification(ProgressNotification pn) {
        progressBar.setProgress(pn.getProgress());
    }

    @Override
    public void handleStateChangeNotification(StateChangeNotification evt) {
        if (evt.getType() == StateChangeNotification.Type.BEFORE_START) {
            stage.hide();
        }
    }
}

Testing

Cellophane answered 1/11, 2022 at 16:44 Comment(0)
O
5

For JavaFX2, you can set a custom preloader. You have complete control over then scene. I haven't used them personally, but this might be what you want. http://docs.oracle.com/javafx/2/deployment/preloaders.htm

Overhand answered 14/8, 2013 at 18:4 Comment(1)
Thank you so much, because of that link I could create a Minimal Reproducible Example (MRE) for others here. Check out my answer: https://mcmap.net/q/1018314/-how-to-create-a-custom-loading-screen-in-javafxCellophane
C
1

If you're setting things up as shown on This blog entry, it looks like the answer would be 'no' - the loading graphic is just part of the overall options that are passed to the applet. Because this applet could be any java code (not just javaFX), there's no way to tie your custom renderer in.

Cookery answered 16/6, 2009 at 19:12 Comment(1)
JavaFX has a native API for preloaders, see my simple example: https://mcmap.net/q/1018314/-how-to-create-a-custom-loading-screen-in-javafxCellophane
O
1

you should use java timer:

Timer tm= new Timer(); 
Stage ilk;
int count;

public  void check() {      

    ilk=new Stage();
    TimerTask mission;

    gorev = new TimerTask() {
        @Override
        public void run() {

            Group root = new Group();     

            Scene scene;
            scene = new Scene(root, 960, 540);
            scene.setFill(Color.BLACK);
            ilk.setScene(scene);
            ilk.setTitle("Splash Screen"); 

            sayac++;
            if(count==5){
                tm.cancel();
                ilk.show();  
            }
        }
    };
    tm.schedule(mission, 0, 2000);
}
Oogonium answered 29/6, 2014 at 20:22 Comment(1)
JavaFX has a native API for preloaders, see my simple example: https://mcmap.net/q/1018314/-how-to-create-a-custom-loading-screen-in-javafxCellophane
C
1

JavaFX preloader class

I have created a very simple preloader screen using native JavaFX APIs. Here it's explained how to do this: https://docs.oracle.com/javafx/2/deployment/preloaders.htm (old but workable examples) - this is newer and seems to be the same: https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/preloaders.html (Newer page and JavaFX version but I don't see the difference).

The older link is easier to read, because of page formatting.

Main class

package YOUR_PACKAGE_NAME;

import javafx.application.Application;

/**
 * Minimal reproducible example (MRE) - Example of a simple JavaFX preloader.
 * Java Main class for starting up the JavaFX application with a call to launch MainApplication.
 * @author Remzi Cavdar - [email protected] - <a href="https://github.com/Remzi1993">@Remzi1993</a>
 */
public class Main {
    public static void main(String[] args) {
        /*
         * The following Java system property is important for JavaFX to recognize your custom preloader class.
         * Which should extend javafx.application.Preloader.
         */
        System.setProperty("javafx.preloader", Preloader.class.getName());
        // Launch the main JavaFX application class.
        Application.launch(MainApplication.class, args);
    }
}

Preloader class

package YOUR_PACKAGE_NAME;

import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * Minimal reproducible example (MRE) - Example of a simple JavaFX preloader class.
 * @author Remzi Cavdar - [email protected] - <a href="https://github.com/Remzi1993">@Remzi1993</a>
 */
public class Preloader extends javafx.application.Preloader {
    private ProgressBar progressBar;
    private Stage stage;

    private Scene createPreloaderScene() {
        progressBar = new ProgressBar();
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(progressBar);
        return new Scene(borderPane, 800, 600);
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        // I also recommend to set app icon: stage.getIcons().add();
        stage.setTitle("YOUR TILE HERE");
        stage.setScene(createPreloaderScene());
        stage.show();
    }

    @Override
    public void handleProgressNotification(ProgressNotification pn) {
        progressBar.setProgress(pn.getProgress());
    }

    @Override
    public void handleStateChangeNotification(StateChangeNotification evt) {
        if (evt.getType() == StateChangeNotification.Type.BEFORE_START) {
            stage.hide();
        }
    }
}

Testing

Cellophane answered 1/11, 2022 at 16:44 Comment(0)
H
-1

For changing the coffee cup icon:

stage.getIcons().add(new Image("images/myimage.png"));

and here is a reference for a very clear preloader screen out there and awesome css too: http://docs.oracle.com/javafx/2/best_practices/jfxpub-best_practices.htm

Herb answered 19/5, 2014 at 0:56 Comment(1)
The examples show there doesn't work anymore (as of 01-11-2022), but the examples show here work: docs.oracle.com/javafx/2/deployment/preloaders.htm and based on that I have made a simple example using the native build in JavaFX preloader API: https://mcmap.net/q/1018314/-how-to-create-a-custom-loading-screen-in-javafxCellophane

© 2022 - 2024 — McMap. All rights reserved.