eclipseosgijavafxpreloader

How to use JavaFX Preloader with stand-alone application in Eclipse?


My IDE is Eclipse and my project is a stand-alone JavaFX application (pure CS architecture with OSGI framework).

How to use preloader thus the preloader would be started before my main application and hid later?

I found some code here:

http://docs.oracle.com/javafx/2/deployment/preloaders.htm#BABGGDJG

But I still don't know how to deploy the preloader with my startup application in an OSGI framework.

I give some code of my startup application below:

public class MyPrjMain extends Application {
    private static Stage primaryStage;

    public void start(final Stage stage) throws BusinessException {
        primaryStage = stage;
    
        init(primaryStage);
    
        primaryStage.show();
    }
}

Solution

  • This is a long answer, the quick answer for the impatient is to download this sample code for displaying a splash page for an intensive startup task and see if it is adaptable to your situation.


    My answer provides general information about Preloader style functionality in JavaFX. Your question specifically mentions Preloader usage in an Eclipse and OSGI environment, but I won't directly address that scenario as I don't use those technologies. Hopefully the general information is still applicable to your scenario.

    1. Java has native support for displaying a splash page when Java is started.

    Advantages and disadvantages:

    + The simplest way to get your standalone application to show a splash image.

    + Can be displayed very quickly => it's an argument input to the VM process, so (presumably) it can be displayed even before the VM itself has fully initialized.

    - Has limited features => only allows display of an image, not other preloader features such as reporting of initialization progress, animation, login prompts etc (unless you make use of AWT APIs)

    - Won't work on all platforms until Java 8 (see issue Mac: Impossible to use -splash: with JavaFX 2.2 and JDK 7).

    2. Preloaders may be used for standalone applications.

    The JavaFX Preloader tutorial has an example in the section 9.3.4 Using a Preloader to Display the Application Initialization Progress. The tutorial provides executable sample code in the LongInitAppPreloader and LongInitApp classes (use the class names I provide in this answer as one name in the tutorial is currently wrong).

    The sample standalone application has a long initialization time and a custom Preloader provides feedback on the progress of the initialization. The sample simulates the long initialization through a Task with a Thread.sleep call, but a real application would be doing something like establishing network connections, retrieving and parsing network data and setting up the initial application Scene.

    Preloaders are not specific to applets and WebStart, but are primarily targeted to those deployment types. The applet and WebStart initialization process is more complex than standalone application initialization, so much of the Preloader documentation is devoted to those more complex scenarios.

    3. You don't need to place a Preloader in a separate JAR.

    You can place the Preloader in the same JAR as your Application class. For large applications dynamically deployed and updated over network loading protocols such as WebStart, placing the Preloader in a seperate JAR makes sense. For standalone applications performing network based initialization, it probably doesn't make much difference and the separate packaging step could be skipped to simplify the build and deployment process.

    4. You can achieve Preloader style functionality without using a Preloader.

    Much (not all) of the Preloader functionality can be achieved without subclassing Preloader.

    You can:

    1. Create a startup Stage in your application's start method.
    2. Place a splash image and ProgressBar in the startup stage.
    3. Have a background task for lengthy application initialization processes.
    4. Report initialization progress back to your startup stage from your background task.
    5. On initialization completion, either:
      • a. Replace the startup stage with a newly created application stage OR
      • b. Replace the contents of the scene in the startup stage with a new scene for your application.

    5b is probably preferred so that you don't need to create multiple windows.

    For examples of this strategy, see my answers to the following questions:

    The related sample code for displaying Progress Monitoring splash screens in JavaFX without using a Preloader is:

    The above code could be refactored to use a Preloader subclass instead, in which case there is a well defined framework for notification of application initialization events and more flexible deployment models (e.g. preloader in seperate jar) are available. However use of a Preloader may be a little complicated. For some implementations, it may not be worth the time to understand the Preloader framework.

    5. WebStart Apps have JNLP support for Splash Images

    (this point is pretty irrelevant and just included for completeness).

    I believe that webstart applications can have a flag in their jnlp file to show the startup image as the webstart application launches, but I've never been able to get that flag to work in a JavaFX 2 application, only in a Swing application and even then it wasn't all that reliable as it would only display the second time the application was launched.