ProgressBar

Live Demo

The ProgressBar component allows visualizing progress of a task. The progress is specified as a floating-point value between 0.0 and 1.0.

progressbar basic

The ProgressBar component

To display upload progress with the Upload component, you can update the progress bar in a ProgressListener.

When the position of a progress bar is done in a background thread, the change is not shown in the browser immediately. You need to use either polling or server push to update the browser. You can enable polling with setPollInterval() in the current UI instance. See “Server Push” for instructions about using server push. Whichever method you use to update the UI, it is important to lock the user session by modifying the progress bar value inside access() call, as illustrated in the following example and described in “Accessing UI from Another Thread”.

Java

  1. final ProgressBar bar = new ProgressBar(0.0f);
  2. layout.addComponent(bar);
  3. layout.addComponent(new Button("Increase",
  4. new ClickListener() {
  5. @Override
  6. public void buttonClick(ClickEvent event) {
  7. float current = bar.getValue();
  8. if (current < 1.0f)
  9. bar.setValue(current + 0.10f);
  10. }
  11. }));

Indeterminate Mode

In the indeterminate mode, a non-progressive indicator is displayed continuously. The indeterminate indicator is a circular wheel in the built-in themes. The progress value has no meaning in the indeterminate mode.

Java

  1. ProgressBar bar = new ProgressBar();
  2. bar.setIndeterminate(true);

progressbar indeterminate

Indeterminate progress bar

Doing Heavy Computation

The progress bar is typically used to display the progress of a heavy server-side computation task, often running in a background thread. The UI, including the progress bar, can be updated either with polling or by using server push. When doing so, you must ensure thread-safety, most easily by updating the UI inside a UI.access() call in a Runnable, as described in “Accessing UI from Another Thread”.

In the following example, we create a thread in the server to do some “heavy work” and use polling to update the UI. All the thread needs to do is to set the value of the progress bar with setValue() and the current progress is displayed automatically when the browser polls the server.

Java

  1. HorizontalLayout barbar = new HorizontalLayout();
  2. layout.addComponent(barbar);
  3. // Create the bar, disabled until progress is started
  4. final ProgressBar progress = new ProgressBar(new Float(0.0));
  5. progress.setEnabled(false);
  6. barbar.addComponent(progress);
  7. final Label status = new Label("not running");
  8. barbar.addComponent(status);
  9. // A button to start progress
  10. final Button button = new Button("Click to start");
  11. layout.addComponent(button);
  12. // A thread to do some work
  13. class WorkThread extends Thread {
  14. // Volatile because read in another thread in access()
  15. volatile double current = 0.0;
  16. @Override
  17. public void run() {
  18. // Count up until 1.0 is reached
  19. while (current < 1.0) {
  20. current += 0.01;
  21. // Do some "heavy work"
  22. try {
  23. sleep(50); // Sleep for 50 milliseconds
  24. } catch (InterruptedException e) {}
  25. // Update the UI thread-safely
  26. UI.getCurrent().access(new Runnable() {
  27. @Override
  28. public void run() {
  29. progress.setValue(new Float(current));
  30. if (current < 1.0)
  31. status.setValue("" +
  32. ((int)(current*100)) + "% done");
  33. else
  34. status.setValue("all done");
  35. }
  36. });
  37. }
  38. // Show the "all done" for a while
  39. try {
  40. sleep(2000); // Sleep for 2 seconds
  41. } catch (InterruptedException e) {}
  42. // Update the UI thread-safely
  43. UI.getCurrent().access(new Runnable() {
  44. @Override
  45. public void run() {
  46. // Restore the state to initial
  47. progress.setValue(new Float(0.0));
  48. progress.setEnabled(false);
  49. // Stop polling
  50. UI.getCurrent().setPollInterval(-1);
  51. button.setEnabled(true);
  52. status.setValue("not running");
  53. }
  54. });
  55. }
  56. }
  57. // Clicking the button creates and runs a work thread
  58. button.addClickListener(new Button.ClickListener() {
  59. public void buttonClick(ClickEvent event) {
  60. final WorkThread thread = new WorkThread();
  61. thread.start();
  62. // Enable polling and set frequency to 0.5 seconds
  63. UI.getCurrent().setPollInterval(500);
  64. // Disable the button until the work is done
  65. progress.setEnabled(true);
  66. button.setEnabled(false);
  67. status.setValue("running...");
  68. }
  69. });

The example is illustrated in Doing heavy work.

progressbar thread

Doing heavy work

CSS Style Rules

CSS

  1. .v-progressbar, v-progressbar-indeterminate {}
  2. .v-progressbar-wrapper {}
  3. .v-progressbar-indicator {}

The progress bar has a v-progressbar base style. The progress is an element with v-progressbar-indicator style inside the wrapper, and therefore displayed on top of it. When the progress element grows, it covers more and more of the animated background.

The progress bar can be animated (some themes use that). Animation is done in the element with the v-progressbar-wrapper style, by having an animated GIF as the background image.

In the indeterminate mode, the top element also has the v-progressbar-indeterminate style. The built-in themes simply display the animated GIF in the top element and have the inner elements disabled.