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.

No comments:

Post a Comment