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

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

Closing/destroying the splash screen

When you finish "starting up" your application you'll want to remove the splash screen and replace it with your application windows, presumably a Java JFrame Because my SplashScreen class is a simple JWindow you can achieve this effect by making the window not visible, as I do in the splashScreenDestruct method shown below:

private void splashScreenDestruct() {
  screen.setScreenVisible(false);
  screen = null;
}

I also set the screen reference to null. This isn't required, but for the purposes of this article, I thought it might clarify my intent.

My Java splash screen "driver" class

That finishes my discussion of the "driver" class that I used to build and test my SplashScreen class. For completeness, here is a listing of the full source code of this class.

package com.devdaily.splashscreen;

import javax.swing.UIManager;
import javax.swing.ImageIcon;

public class SplashScreenMain {

  SplashScreen screen;

  public SplashScreenMain() {
    // initialize the splash screen
    splashScreenInit();
    // do something here to simulate the program doing something that
    // is time consuming
    for (int i = 0; i <= 100; i++)
    {
      for (long j=0; j<50000; ++j)
      {
        String poop = " " + (j + i);
      }
      // run either of these two -- not both
      screen.setProgress("Yo " + i, i);  // progress bar with a message
      //screen.setProgress(i);           // progress bar with no message
    }
    splashScreenDestruct();
    System.exit(0);
  }

  private void splashScreenDestruct() {
    screen.setScreenVisible(false);
  }

  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);
  }

  public static void main(String[] args)
  {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    new SplashScreenMain();
  }

}

Java Splash Screen source code

You can also download the SplashScreenMain.java source code.

 

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

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