Splash Screen with Javafx

Splash Screen code walk through

Splash Screens are needed for applications we develop in Javafx to show the user about the progress of any background processes that needs to be completed before the GUI can come up completely. Recently I came across one such problem. I had an application that had huge amounts of background work to complete before the Stage and scene could be set for the GUI to come up. Therefore I looked up into the SplashScreen JavaAPI but it does not turn out to be very useful since it only displays the image mentioned in Manifest file or the one passed through the setImageURL() method. I felt rather than using this I can create my own Splash Screen.

To create a Splash screen I have created a class ShowWindow which extends the Jwindow. This basically constructs the splash screen having 2 components. 1) Image to be displayed to the user. 2) ProgressBar to the user displaying a progress and a titled border around the progress bar to display what is completed at a given time period. These 2 components are packed together in a content pane and displayed to the user when the application loads. Therefore here the Main application GUI is Javafx(desktop and desktop dock) but the splash screen is a java swing application. So it appears as if all of these are part of javafx Main GUI.

The ShowWindow class also implements the Runnable interface in order for the splash screen to run in its own daemon thread, so that it does not bog down the main thread. The daemon thread once started is put in a loop to update the progress bar and the progress titles as soon as it receives inputs on the amount of completion on the background process.

As an example below we have the front end GUI written in Javafx and it loads completely a user desktop with a dock at the top. Usually when we develop a real time applications we have lot of processing needed at the back end, therefore it sometimes becomes necessary to display a Splash screen with a progress bar to display the status and progress.

Here the program starts with the run method of a stage which is overridden. I have first created a Stage applying a background image to it so that the stage background is not blank during the background process.  At this point by default I have set the progress bar value to 10% and String displayed as “Stage Set” since the stage creation is done at this point.

I have created a Background function/method which could contain calls to background processes. For now I have put thread sleeps for 2 second intervals.(simulating background java function call which may take more or less 2 secs using thread sleeps). As a Java Developer, we start demanding those things which are generally done by the concept of multi-threading in Java. Remember, JavaFX is single threaded application. Therfore we can achieve multithreading using Timeline.

Creating a Timeline variable which calls the background function/method makes sure that a seperate thread runs the background function/method containing background processes earlier created. Now when each of the task in the background function/method is completed we can update the progress bar with new values. Remember that Progress bar displayed to the user is done using swings in a seperate daemon thread. Therefore we need to update the progressbar value with new incremented values as and when tasks are completed. To achive this we can create a static public progress bar value int variable which can be updated with new values so that the deamon thread that runs in a loop sees the updates done to this variable and then updates the progress bar’s progress.

Press on the Launch button to launch the application using webstart.

For windows users pressing on the Launch button would start the Application in java web start.

However Linux users may face the problem of just jnlp the file being downloaded onto the system. However we can still get the application started by opening the terminal and setting the JDK home to 1.6 and then start the application by using

export JAVA_HOME=/usr/jdk1.6.0_18 and then set the PATH variable export PATH=/usr/jdk1.6.0_18/bin:$PATH

After doing the above just execute:-   javaws SplashScreen.jnlp

3 Comments

Filed under Javafx Code Demos

3 responses to “Splash Screen with Javafx

  1. Thanks for the information

  2. nicOL

    where can i get or learn the coding?….

    please help..

Leave a comment