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

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

My Java splash screen source code

Next up we'll dig into our Java SplashScreen class to see how I've actually implemented the desired behavior.

First, it's important to note that I wrote this during my lazier days, and created the UI using JBuilder, hence the ugly jbInit method. (I used to keep this method for JBuilder compatibility, but now that I use Eclipse I'll be glad to delete this if it causes grief for too many people.)

Other than that quirk, I think you'll see that the code is straightforward. Looking at the interface of the class, i.e., the public methods, I've defined two setProgress methods for the progress bar, a setProgressMax method, and a setVisible method. You saw how to use each of these method calls earlier in this tutorial.

Perhaps the only important thing to note about these methods is the use of the SwingUtilities.invokeLater method. These calls are made to ensure that the user interface is updated properly through the Java/Swing Event Dispatch Thread (EDT). (If you're not familiar with Swing programming, it's important to note that you always want to update Swing GUI components via the EDT.)

Here is the source code for my Java splash screen class:

package com.devdaily.splashscreen;

import javax.swing.*;
import java.awt.*;

public class SplashScreen extends JWindow {
  BorderLayout borderLayout1 = new BorderLayout();
  JLabel imageLabel = new JLabel();
  JPanel southPanel = new JPanel();
  FlowLayout southPanelFlowLayout = new FlowLayout();
  JProgressBar progressBar = new JProgressBar();
  ImageIcon imageIcon;

  public SplashScreen(ImageIcon imageIcon) {
    this.imageIcon = imageIcon;
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  // note - this class created with JBuilder
  void jbInit() throws Exception {
    imageLabel.setIcon(imageIcon);
    this.getContentPane().setLayout(borderLayout1);
    southPanel.setLayout(southPanelFlowLayout);
    southPanel.setBackground(Color.BLACK);
    this.getContentPane().add(imageLabel, BorderLayout.CENTER);
    this.getContentPane().add(southPanel, BorderLayout.SOUTH);
    southPanel.add(progressBar, null);
    this.pack();
  }

  public void setProgressMax(int maxProgress)
  {
    progressBar.setMaximum(maxProgress);
  }

  public void setProgress(int progress)
  {
    final int theProgress = progress;
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        progressBar.setValue(theProgress);
      }
    });
  }

  public void setProgress(String message, int progress)
  {
    final int theProgress = progress;
    final String theMessage = message;
    setProgress(progress);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        progressBar.setValue(theProgress);
        setMessage(theMessage);
      }
    });
  }

  public void setScreenVisible(boolean b)
  {
    final boolean boo = b;
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        setVisible(boo);
      }
    });
  }

  private void setMessage(String message)
  {
    if (message==null)
    {
      message = "";
      progressBar.setStringPainted(false);
    }
    else
    {
      progressBar.setStringPainted(true);
    }
    progressBar.setString(message);
  }
}

Java splash screen with progress bar - conclusion

For Swing/GUI applications that take a little time to start up, a Java splash screen provides a professional touch to your application, and more importantly, provides feedback to the user during the startup process. This makes for happy users. On top of that, a splash screen with an active progress bar -- as shown in this article -- provides even more feedback to your users. This is useful not only for typical application startup, but can even help you for times when you need to debug the application startup process.

Related Java splash screen content

Here are links to the other "Java splash screen" pages in this tutorial: