A Java splash screen with a progress bar (Part 2)

<< Back to "Java splash screen with progress bar, part 1"

Initializing our Java splash screen

Initializing the splash screen should be simple. We need to get an image to display on our Java splash screen, construct the splash screen, and then display it. Here's what a "splash screen initialization" method might look like:

private void splashScreenInit() {
  ImageIcon myImage = new ImageIcon(com.devdaily.splashscreen.SplashScreenMain.class.getResource("SplashImage.gif"));
  screen = new SplashScreen(myImage);
  screen.setLocationRelativeTo(null);
  screen.setProgressMax(100);
  screen.setScreenVisible(true);
}

As you can see from this listing, the first thing you need to do is create an image. Next, the image is passed into the constructor of the SplashScreen. Positioning of the splash screen is handled by the setLocationRelativeTo call. The maximum value of the splash screen progress bar is handled in the setProgressMax call. This method simply calls the JProgressBar setMaximum method. Finally, the screen is made visible to the user with the setScreenVisible method.

This is all we have to do to initialize the splash screen. After this method is called our splash screen will be visible to the user. The next thing we want to do is start updating the progress bar as we initialize things in our application.

Updating the splash screen progress bar

In a real world application we would next start initializing things (like our database connections) as our application starts up. For my little demo the only thing I can do is simulate that, but hopefully you'll get enough from this demo that you'll be able to apply it to your applications. Your calls to our Java splash screen class will be the same, but your calls won't be from within a nice little for loop like my example.

Here's the code I use in my SplashScreenMain class that simulates the calls you'll be making.

for (int i = 0; i <= 100; i++)
{
  for (long j=0; j<50000; ++j)
  {
    String poop = " " + (j + i);
  }
  // run either of these two -- but not both
  screen.setProgress("Yo " + i, i);  // progress bar with a message
  //screen.setProgress(i);           // progress bar with no message
}

This contrived for loop takes a little while to run (which is the whole purpose of my demo). Nothing in the for loop really matters, except for the screen.setProgress method calls. This is where I'm updating the splash screen's progress bar.

As you can see, there are two different ways to update the progress bar status. In the example that is not commented out, I'm passing both a message and a value to the progress bar. This results in a progress bar that looks like this:

A Java splash screen with progress bar (JProgressBar)

If instead I were to comment out that line of code and replace it with the line beneath it (i.e., screen.setProgress(i), the progress bar will look like this:

A Java splash screen with no progress bar

You can take either approach in your application, but I personally prefer the text/message approach, letting the user know what the application is doing.

<< Previous: "Java splash screen with progress bar, part 1"

Next: Java splash screen with progress bar, part 3 >>