Hi, friends today I’ve got something graphics and animation gift for you.
I know that JavaFX 2 has really Rich Interface in the Application that’s why many enterprises who are using the Swing as their core GUI are intending to divert to JavaFX 2 which is now becoming more stable and clear API. I just love doing javafx cuz it makes me fun and really interesting. The Application we develop in javafx can be huge so we need to load them during startup of the Application. JavaFX has concept of customizing the Preloader which we actually see those progress bar while we are loading any javafx application. Most of the enterprises don’t want to add the default they want something unique and some more eye caching loading so that user won’t feel bore while the application is loading. This is somehow the way of making the user’s eye hypnotized to application 🙂 .
So I’m today talking about same preloader which is actually customized animated graphics. Before starting the preloader we need to know about the flow of the preloader in Application Startup Life Cycle.
Basic Preloader Concept
Now up there you can see how the Preloader are initialized during the startup of the application. The full tutorial of the Preloader is available in Preloaders Tutorial And the full API of Preloader
The above picture describes the step wise lifecycle as below:
- Loading and Initializing Preloader
Here the loading of the Preloader is done. It loads the jar which contains the preloader class.
- Preloader Started
In this step the preloader will be loaded completely and the Preloader Class’s start() function is invoked by the JavaFX Engine.
- Loading Application Resources
In this part the main application jar will be downloading. And the progress notification are send to the Preloader class. During this phase the notifyPreloader(PreloaderNotification) is invoked by the Runtime Engine and sends the Notification to the Preloader class continously.
- Application Initialization
Now at this step the application jar will be fully loaded in client’s computer. And the main Application’s init() is being called. Before starting this init(). It sends the BEFORE_INIT state to the Preloader class.
- Application Started
Finally the main Application’s start() function is invoked by the Java engine. But before starting that function it sends the notification of the BEFORE_START to the Preloader class. In this phase we actually hide the stages or remove the preloader’s stage.
Ok now Let’s hook over the Preloader Class function.
handleStateChangeNotification(Preloader.StateChangeNotification);
This function is the main function which helps to know the state of the Preloader. There currently three states of the Preloader.
- BEFORE_INIT
- BEFORE_LOAD
- BEFORE_START
Now we are currently only dealing with the BEFORE_START which actually means that the start() function of the main Application is going to be loaded.
handleProgressNotification(Preloader.ProgressNotification);
Now this is another important function which is helpful for showing the Progress of the Application. Most of the application use this function to show how much the data is loaded. It shows the progress from 0.0(min) to 1.0(max) .
Implementation of Preloader
Software Used: Netbeans 7.2 (Beta)
Firstly to make our custom Preloader we will need to make new Project from Netbeans.
Click on New Project> JavaFX >JavaFX Application.
Name your project name . (I’ve named FancyPreloader)
Now check on the “Create Custom Preloader Checkbox”
You will see now two projects being created by Netbeans.
- FancyPreloader
- FancyPreloader-Preloader
All we have to do is inside FancyPreloader-Preloader class of FancyPreloader-Preloader project. All the implementation of the Preloader is done here.
Let’s move to the coding part.
 FancyPreloader_Preloader.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class FancyPreloader_Preloader extends Preloader { Stage stage; GUI gui; private Scene createPreloaderScene() { gui = new GUI(); try { gui.buildComponents(); } catch (IOException ex) { Logger.getLogger(FancyPreloader_Preloader.class.getName()).log(Level.SEVERE, null, ex); } return gui.getScene(); } @Override public void start(Stage stage) throws Exception { this.stage = stage; stage.setScene(createPreloaderScene()); stage.show(); gui.animate(); gui.explodeAnimate(); } @Override public void handleStateChangeNotification(StateChangeNotification scn) { if (scn.getType() == StateChangeNotification.Type.BEFORE_START) { stage.hide(); } } @Override public void handleProgressNotification(ProgressNotification pn) { gui.update(pn.getProgress()); } } |
In above code I’ve used the class GUI which contains the GUI building class of the Preloader. The details of the GUI class will be included in next post. Now here we have used two function they are :
1 2 3 4 5 6 7 |
@Override public void handleStateChangeNotification(StateChangeNotification scn) { if (scn.getType() == StateChangeNotification.Type.BEFORE_START) { stage.hide(); } } |
In above function we have made the current FancyPreloader_Preloader’s Stage to be hidden so that the real Application stage get’s visible to the user.
1 2 3 4 5 |
@Override public void handleProgressNotification(ProgressNotification pn) { gui.update(pn.getProgress()); } |
In above function we have overridden one more function; this is the function which helps to show the Progress of the preloader.
If you want to know what the GUI class actually does then it’s given below
GUI.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/** * All Animation is started by this function */ public void animate() /** * Build all the main component of this animation * @throws IOException */ public void buildComponents() /** * When the loading finishes then all animation are being stopped . */ public void dispose() /** * When neon loading get's loaded then the explode of neon is done from this function */ public void explodeAnimate() /** * Returns the instance of the scene * @return */ public Scene getScene() /** * This is the main update function which is triggered from preloader class * and this makes the gui to show some info the loading progress * @param percent */ public void update(double percent) |
In coming next blog post we ‘ll be learning about how to make the preloader looks more fancy using Animation and SVG stuffs.All the details of the GUI class will be included in next blog post.
Keep in touch .Happy coding and have a 🙂 good day.
Looks interesting!!!
Bring it on!:)
Could you, by any chance, elaborate on how to use this kind oh custom preloader with Web Start (JNLP), in some of your future posts?
Pingback: Java desktop links of the week, July 30 | Jonathan Giles
Pingback: JavaFX links of the week, July 30 | JavaFX News, Demos and Insight // FX Experience
ok I’ll try to explain them in next part too.
Pingback: Fancy Preloader Part 2 « Java and FX
Hi, I would like to ask about Java Fx preloader. If I want to try some preloader, I must have some application behind to load itself (and which app is good for testing, if its needed), or it is possible run the preloader indenpendently, just to see, how it looks like, or how its possible to load it etc. Thx very much.
P.S. Sorry for bad eng.
Hi ali,
For better feel of preloader stuffs place some heavy files like video, or big files inside the javafx project and attach the preloader with them. Even if you are in local computer execution you will find the preloader clearly by this approach.
Thanks
Great post.
Currenlty, i am developeing Desktop Application with JavaFx and i need to convert into .exe file. i am dacing problem in Converting my appication into JAr file …………..
Please Help me
Hi Nithin ,
Have you tried with “install4j” which can convert your jar file to native executable file.
THANKS
Narayan