In this tutorial I look at how to create a splash screen in a Java application. At the end of the discussion I'll share all of the Java classes that are needed to implement your own Java splash screen.
To make it a little more complicated - and to make your application better - I'll create a splash screen with a progress bar that can be updated from your Java code. The progress bar helps provide more feedback to your users, letting the user know that your application is really doing something during the application startup process.
Java splash screen - introduction
I have to admit to a certain bias when writing this article. I hate it when slow-starting applications don't provide any feedback to me during the startup process. After starting an application and waiting a few seconds without any feedback, my brain naturally starts wondering "Did I double-click that? Maybe I didn't.", or "Hmm, I wonder if something is wrong, it doesn't look like the application is starting up." Being impatient, I either re-click the startup icon or do other things that end up causing more harm than good, all because I'm not given any feedback that the application is actually in the process of starting up.
You can obviously recover from this bad start by having a great application, but why skip something that's pretty easy, and gives users a warm fuzzy about your app? If you have an application that doesn't display a frame almost immediately, I recommend that you show the user a splash screen to show them that the application is really starting. And better yet, provide a splash screen with a progress bar to let the user know that the application is really doing something as it starts up.
So, with those thoughts in mind, let's start creating a Java splash screen that can be used/reused in your Java applications.
Getting started with our splash screen
In the words of Mr. Covey, let's "begin this article with the end in mind." To that end, let's look at a splash screen as the user will see it. While my graphic is a little lame (I'm a developer, not an artist), this figure shows what I want the splash screen to look like when it is visible:
As you can see from the figure, I've implemented this splash screen as a JWindow. I want to have a good-looking image that represents my Java application taking most of the space, and a progress bar (JProgressBar) updating in an area below the image. This is a nice, standard splash screen implementation (and hey, if you want to change it for your Java application, the source code is free).
Java splash screen - the API
Now that I know what it should look like, I now want to think about the application from the developer perspective. As a Java developer, I know that I want my application to start up like this:
main()method is called.
- I want to display the splash screen as fast as possible.
- I want to update the progress bar on the splash screen during time-consuming startup events, like connecting to a database, reading configuration files, etc.
- As soon as the application is ready to start I want to destroy the splash screen and show the main frame of the application.
This gives me a little idea of what my code is going to look like. From that discussion I know I want the following methods in my splash screen API:
- I want a method to initialize the splash screen.
- I want a method to display it.
- I want a method to update its progress bar.
- I want a method to destroy (close) the splash screen.
Given that, let's start writing some code, assuming these method calls already exist.