up previous next contents
Next: Conclusion Up: How to create and Previous: The full "driver" class   Contents

The SplashScreen implementation

Next up we'll look at the SplashScreen class to see how I've actually implemented the desired behavior.

First, it's important to note that I'm lazy, and created the UI using JBuilder, hence the jbInit method. If you've seen my code before, you'll know that I keep the code like this so I can easily bring it back into JBuilder, and use it in their visual designer.

Other than that quirk, I think you'll see that the code is relatively straightforward. Looking at the interface of the class, i.e., the public methods, I've defined two setProgress methods, a setProgressMax method, a setVisible method. You saw how to use each of these in the previous sections of this document.

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 user interface thread. I can't say that I'm an "expert" in this area, but I'm pretty sure this is the proper way to update the UI. (Of course, if I'm wrong, just send me an email, and I'll update this article.)

For what it's worth, I've also implemented a private setMessage method. This shouldn't matter too much to you, as a consumer of the SplashScreen class. For the most part I created it to simplify the implementation of one of the setProgress methods.

This is the source code for my SplashScreen.java 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);
  }

}


up previous next contents
Next: Conclusion Up: How to create and Previous: The full "driver" class   Contents