How to show download Progress Bar in Java



Sometimes a task running within a program might take a while to complete. A user-friendly application provides some indication to the user that the task is occurring, how long the task might take, and how much work has already been done.  It also shows the amount of downloaded stuff and remaining stuff in either percentages or in actual measures. This is called determinate progress Bar.

Sometimes you can't immediately determine the length of a long-running task, or the task might stay stuck at the same state of completion for a long time. You can show work without measurable progress by putting the progress bar in indeterminate mode. A progress bar in indeterminate mode displays animation to indicate that work is occurring and is in progress. Such kind of progress Bar are called indeterminate Progress Bar.

In this sample tutorial I am going to give you an example of determinate progress bar that will show the downloaded size of file and total size of file. The progress Bar should look like this:

This sample example is most suited, when a user want to view or download PDF file from any online application and want some indication that my downloading is in progress and how much stuff has been downloaded. Such kind of progress Bars is more user friendly and user I aware of the downloading in progress process.




Sample Code:


import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class BrowserProgress {

       // You can make this URL dynamic per user requirement
       public static String DOWNLOAD_URL = "http://www.programmersheaven.com/ebooks/csharp_ebook.pdf";

       public static void main(String[] args) {
              Display display = new Display();
              Shell shell = new Shell(display);
              shell.setSize(1200, 700);

              Browser browser = new Browser(shell, SWT.NONE);
              browser.setBounds(5, 5, 1200, 700);

              final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
              progressBar.setBounds(5, 650, 600, 20);

              browser.addProgressListener(new ProgressListener() {
                     public void changed(ProgressEvent event) {
                           if (event.total == 0) {
                                  return;
                           }

                           int ratio = event.current * 100 / event.total;
                           progressBar.setSelection(ratio);
                     }

                     public void completed(ProgressEvent event) {
                           progressBar.setSelection(0);
                     }

              });

              // Set URL here
              browser.setUrl(DOWNLOAD_URL);
              shell.open();

              while (!shell.isDisposed()) {
                     if (!display.readAndDispatch())
                           display.sleep();
              }
              display.dispose();
       }
}

Note:

In order to run the above code you will have to download the org.eclipse.swt.win32.win32.x86_3.7.1.v3738a.jar and will need to import as Jar file into your project.

Generating Barcode in Java using Barcode4J API

Barcode4J is an open source JAVA API which is used to generate flexible barcodes. Using Barcode4J one can generate almost all type of barcode formats like Bar Code 11, Bar Code 39, Bar Code 93, Bar Code 128, Bar Code 128A, and Bar Code 128B. 

If you want to know details about Barcode4J, you can find some details here. Below is a sample code demo that demonstrates how to generate barcode of 3 of 9 format (Bar-Code 39) and Bar-Code128.


MyBarCodeUtility.java



import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

public class MyBarCodeUtility {

  private String barCodePath = "D:/";
  

  public void createBarCode128(String fileName) {
    try {
      Code128Bean bean = new Code128Bean();
      final int dpi = 160;

      //Configure the barcode generator
      bean.setModuleWidth(UnitConv.in2mm(2.8f / dpi));

      bean.doQuietZone(false);

      //Open output file
      File outputFile = new File(barCodePath + fileName + ".JPG");

      FileOutputStream out = new FileOutputStream(outputFile);
    
      BitmapCanvasProvider canvas = new BitmapCanvasProvider(
          out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

      //Generate the barcode
      bean.generateBarcode(canvas, fileName);
   
      //Signal end of generation
      canvas.finish();
    
      System.out.println("Bar Code is generated successfully…");
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }



  public void createBarCode39(String fileName) {

        try {
          Code39Bean bean39 = new Code39Bean();
          final int dpi = 160;

          //Configure the barcode generator
          bean39.setModuleWidth(UnitConv.in2mm(2.8f / dpi));

          bean39.doQuietZone(false);

          //Open output file
          File outputFile = new File(barCodePath + fileName + ".JPG");
        
          FileOutputStream out = new FileOutputStream(outputFile);
        

          //Set up the canvas provider for monochrome PNG output
          BitmapCanvasProvider canvas = new BitmapCanvasProvider(
              out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

          //Generate the barcode
          bean39.generateBarcode(canvas, fileName);
       
          //Signal end of generation
          canvas.finish();
        
          System.out.println("Bar Code is generated successfully…");
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
      }

  public static void main (String str[])
  {
      MyBarCodeUtility barCodeUtil = new MyBarCodeUtility();
    
      // This will generate Bar-Code 3 of 9 format
      barCodeUtil.createBarCode39("naeemgik - 12345");
    
      // This will generate Bar-Code 128 format
      barCodeUtil.createBarCode128("0123456789");
    
  }
}



After executing the above code snippet two Bar Codes should generate at mentioned path of your hard drive. It should look like:


Bar-Code 39 format
                                  
Bar-Code 128 format