Progress splash screen in java with simulate mode


Today I will share a little piece of code … a splash screen with a progress bar feature.

The splash screen support three modes:

  • Indeterminate – animated percentage of completion
  • Determinate – defined percentage of completion
  • Determinate + Simulation – define a FROM and TO percentage and the progress bar will run automatically  (done with the  autoProgressExecutor)

package de.ahoehma;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

class ProgressSplashScreen extends JWindow {

   private static final long serialVersionUID = 935801891530361293L;
   private static final Color COLOR_PROGRESS_BACKGROUND = new Color(160, 182, 192);
   private static final Color COLOR_PROGRESS_FORGROUND = new Color(215, 224, 227);
   private final BufferedImage splashScreenImage;
   private final JProgressBar progressbar;
   private final ExecutorService autoProgressExecutor = Executors.newFixedThreadPool(1);

   public ProgressSplashScreen(final InputStream theResourceAsStream) {
     this(theResourceAsStream, -1, -1);
   }

   public ProgressSplashScreen(final InputStream theResourceAsStream,
                             final int theMin, final int theMax) {
     super();
     try {
       splashScreenImage = ImageIO.read(theResourceAsStream);
     } catch (final IOException e) {
       throw new IllegalArgumentException(String.format("Can't load splashscreen"), e);
     }
     final JPanel contentPanel = new JPanel(new BorderLayout());
     contentPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
     if (theMin != -1 && theMax != -1) {
       progressbar = new JProgressBar(SwingConstants.HORIZONTAL, theMin, theMax);
     } else {
       progressbar = new JProgressBar(SwingConstants.HORIZONTAL);
       progressbar.setIndeterminate(true);
     }
     progressbar.setBackground(COLOR_PROGRESS_BACKGROUND);
     progressbar.setForeground(COLOR_PROGRESS_FORGROUND);
     progressbar.setStringPainted(true);
     final JLabel label = new JLabel(new ImageIcon(splashScreenImage));
     contentPanel.add(label, BorderLayout.CENTER);
     contentPanel.add(progressbar, BorderLayout.SOUTH);
     add(contentPanel);
     pack();
     setLocationRelativeTo(null);
     setAlwaysOnTop(true);
   }

   public void close() {
     setVisible(false);
    autoProgressExecutor.shutdownNow();
    dispose();
   }

   public void showProgress(final int theValue) {
     setVisible(true);
     progressbar.setValue(theValue);
     if (progressbar.getValue() == 100) {
       setVisible(false);
     }
   }

   public void showProgress(final int theValueTo, final int theEstimatedTimeInSeconds) {
     showProgress(progressbar.getValue(), theValueTo, theEstimatedTimeInSeconds);
   }

   public void showProgress(final int theValueFrom, final int theValueTo,
                                 final int theEstimatedTimeInSeconds) {
     setVisible(true);
     autoProgressExecutor.execute(new Runnable() {
         public void run() {
           int numberOfSteps = theValueTo - theValueFrom;
           long timeToWait = TimeUnit.SECONDS.toMillis(theEstimatedTimeInSeconds)
                                    / numberOfSteps;
           for (int i = theValueFrom; i <= theValueTo; i++) {
               progressbar.setValue(i);
               try {
                 TimeUnit.MILLISECONDS.sleep(timeToWait);
               } catch (final InterruptedException e) {
           // ignore
         }
       }
       if (progressbar.getValue() == 100) {
         setVisible(false);
       }
     }
   });
 }
}

Usage:

package de.ahoehma;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

public class ProgressSplashScreenTest {

  public static void main(final String[] args) throws InterruptedException {

    final InputStream imageStream = ProgressSplashScreenTest.class.getResourceAsStream("splashscreen.jpg");
    final ProgressSplashScreen splashScreen = new ProgressSplashScreen(imageStream);
    splashScreen.showProgress(25);
    TimeUnit.SECONDS.sleep(2);
    splashScreen.showProgress(50);
    TimeUnit.SECONDS.sleep(2);
    splashScreen.showProgress(75);
    TimeUnit.SECONDS.sleep(2);
    splashScreen.showProgress(100);
    splashScreen.close();

    final InputStream imageStream2 = ProgressSplashScreenTest.class.getResourceAsStream("splashscreen.jpg");
    final ProgressSplashScreen splashScreen2 = new ProgressSplashScreen(imageStream2, 1, 100);
    TimeUnit.SECONDS.sleep(2);
    splashScreen2.showProgress(25);
    TimeUnit.SECONDS.sleep(2);
    splashScreen2.showProgress(75);
    TimeUnit.SECONDS.sleep(2);
    splashScreen2.showProgress(100);
    TimeUnit.SECONDS.sleep(2);
    splashScreen2.close();

    final InputStream imageStream3 = ProgressSplashScreenTest.class.getResourceAsStream("splashscreen.jpg");
    final ProgressSplashScreen splashScreen3 = new ProgressSplashScreen(imageStream3);
    splashScreen3.showProgress(25, 1);
    TimeUnit.SECONDS.sleep(1);
    splashScreen3.showProgress(25, 75, 2);
    TimeUnit.SECONDS.sleep(2);
    splashScreen3.showProgress(90, 2);
    TimeUnit.SECONDS.sleep(2);
    splashScreen3.showProgress(100, 2);
    TimeUnit.SECONDS.sleep(2);
    splashScreen3.close();

    final InputStream imageStream4 = ProgressSplashScreenTest.class.getResourceAsStream("splashscreen.jpg");
    final ProgressSplashScreen splashScreen4 = new ProgressSplashScreen(imageStream4, 1, 100);
    splashScreen4.showProgress(25, 1);
    TimeUnit.SECONDS.sleep(1);
    splashScreen4.showProgress(25, 75, 2);
    TimeUnit.SECONDS.sleep(2);
    splashScreen4.showProgress(90, 2);
    TimeUnit.SECONDS.sleep(2);
    splashScreen4.showProgress(100, 2);
    TimeUnit.SECONDS.sleep(2);
    splashScreen4.close();
  }
}

Examples

😀