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?

David Koelle
  • 20,726
  • 23
  • 93
  • 130
  • This should be easily doable if you are using java web-start to launch your application. In the JNLP file, you can mention: ` `. This is under the `` tag. – Aspirant Dec 09 '13 at 18:54
  • 1
    This question is old and the answers too, but I have provided a very simple workable example down below: https://stackoverflow.com/a/74279131/10686802 – Remzi Cavdar Nov 01 '22 at 16:52

5 Answers5

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

Brian Blonski
  • 1,762
  • 18
  • 23
  • 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://stackoverflow.com/a/74279131/10686802 – Remzi Cavdar Nov 01 '22 at 16:46
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.

Atiaxi
  • 1,637
  • 1
  • 13
  • 18
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);
}
Morchul
  • 1,987
  • 1
  • 7
  • 21
drojokef
  • 425
  • 1
  • 5
  • 15
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 - ict@remzi.info - <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 - ict@remzi.info - <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

Remzi Cavdar
  • 135
  • 1
  • 13
-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

ufukomer
  • 1,021
  • 1
  • 14
  • 16
  • The examples show there doesn't work anymore (as of 01-11-2022), but the examples show here work: https://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://stackoverflow.com/a/74279131/10686802 – Remzi Cavdar Nov 01 '22 at 16:50