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

2

11

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();
    }
}
Vivanvivarium answered 28/2, 2013 at 2:4 Comment(0)
F
34

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.

  • This works using the -splash:<image> VM switch.

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.

Firebrick answered 1/3, 2013 at 0:57 Comment(1)
The problem in OSGi is that you are not starting with your own Jar but you start up the OSGi-System. I think the best solution is to use a combination of Java-Splash and your own startup stage. I still believe the main purpose of the Preloader is and has always been the Applet/Webstart usecase.Arielariela
A
1

IMHO a Preloader only makes sense when you are running as an applet or webstart because the preloader can be packaged as an extra Jar which is downloaded first and executed while the rest of your application is downloaded in the background.

So my suggestion would be to open a stage at the first point in time when you get a Stage and e.g. display a splash.

Arielariela answered 28/2, 2013 at 8:11 Comment(3)
Thanks tomsontom, you mean that Preloader is not for stand-alone javaFX application but only for "applet or webstart" ? I have to implement the "Splash" with a stage? Jface has a AbstractSplashHandler for Splash page, it's really good...Vivanvivarium
Yes and yes but I would use Javas own support for splash image display and replace it with a stage if interactivity is needed - I also think the native packager has splash-screen support! You should not make use of the Eclipse Launcher and Splash stuff because this e.g. leads on mac to a dead lock for the JavaFX startup. I'm going to take a look at the splash situation for a customer in the next few weeks so it is not unlikely that you'll see very soon classes in e(fx)clipse codebase giving you easy access to it.Arielariela
I'm going to take a look at the splash situation for a customer in the next few weeks so it is not unlikely that you'll see very soon classes in e(fx)clipse codebase giving you easy access to it. ---waiting for your gooood news. :) thanks!Vivanvivarium

© 2022 - 2024 — McMap. All rights reserved.