You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-progressbar.asciidoc 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ---
  2. title: ProgressBar
  3. order: 27
  4. layout: page
  5. ---
  6. [[components.progressbar]]
  7. = [classname]#ProgressBar#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/interaction/progress-bar"]
  11. endif::web[]
  12. The [classname]#ProgressBar# component allows visualizing progress of a task.
  13. The progress is specified as a floating-point value between 0.0 and 1.0.
  14. [[figure.components.progressbar.basic]]
  15. .The [classname]#ProgressBar# component
  16. image::img/progressbar-basic.png[width=30%, scaledwidth=70%]
  17. To display upload progress with the [classname]#Upload# component, you can
  18. update the progress bar in a [interfacename]#ProgressListener#.
  19. When the position of a progress bar is done in a background thread, the change
  20. is not shown in the browser immediately. You need to use either polling or
  21. server push to update the browser. You can enable polling with
  22. [methodname]#setPollInterval()# in the current UI instance. See
  23. <<dummy/../../../framework/advanced/advanced-push#advanced.push,"Server Push">>
  24. for instructions about using server push. Whichever method you use to update the
  25. UI, it is important to lock the user session by modifying the progress bar value
  26. inside [methodname]#access()# call, as illustrated in the following example and
  27. described in
  28. <<dummy/../../../framework/advanced/advanced-push#advanced.push.running,"Accessing UI from Another Thread">>.
  29. [source, java]
  30. ----
  31. final ProgressBar bar = new ProgressBar(0.0f);
  32. layout.addComponent(bar);
  33. layout.addComponent(new Button("Increase",
  34. new ClickListener() {
  35. @Override
  36. public void buttonClick(ClickEvent event) {
  37. float current = bar.getValue();
  38. if (current < 1.0f)
  39. bar.setValue(current + 0.10f);
  40. }
  41. }));
  42. ----
  43. [[components.progressbar.indeterminate]]
  44. == Indeterminate Mode
  45. In the indeterminate mode, a non-progressive indicator is displayed
  46. continuously. The indeterminate indicator is a circular wheel in the built-in
  47. themes. The progress value has no meaning in the indeterminate mode.
  48. [source, java]
  49. ----
  50. ProgressBar bar = new ProgressBar();
  51. bar.setIndeterminate(true);
  52. ----
  53. [[figure.components.progressbar.indeterminate]]
  54. .Indeterminate progress bar
  55. image::img/progressbar-indeterminate.png[width=15%, scaledwidth=40%]
  56. ifdef::web[]
  57. [[components.progressbar.thread]]
  58. == Doing Heavy Computation
  59. The progress bar is typically used to display the progress of a heavy
  60. server-side computation task, often running in a background thread. The UI,
  61. including the progress bar, can be updated either with polling or by using
  62. server push. When doing so, you must ensure thread-safety, most easily by
  63. updating the UI inside a [methodname]#UI.access()# call in a
  64. [interfacename]#Runnable#, as described in
  65. <<dummy/../../../framework/advanced/advanced-push#advanced.push.running,"Accessing
  66. UI from Another Thread">>.
  67. In the following example, we create a thread in the server to do some "heavy
  68. work" and use polling to update the UI. All the thread needs to do is to set the
  69. value of the progress bar with [methodname]#setValue()# and the current progress
  70. is displayed automatically when the browser polls the server.
  71. [source, java]
  72. ----
  73. HorizontalLayout barbar = new HorizontalLayout();
  74. layout.addComponent(barbar);
  75. // Create the bar, disabled until progress is started
  76. final ProgressBar progress = new ProgressBar(new Float(0.0));
  77. progress.setEnabled(false);
  78. barbar.addComponent(progress);
  79. final Label status = new Label("not running");
  80. barbar.addComponent(status);
  81. // A button to start progress
  82. final Button button = new Button("Click to start");
  83. layout.addComponent(button);
  84. // A thread to do some work
  85. class WorkThread extends Thread {
  86. // Volatile because read in another thread in access()
  87. volatile double current = 0.0;
  88. @Override
  89. public void run() {
  90. // Count up until 1.0 is reached
  91. while (current < 1.0) {
  92. current += 0.01;
  93. // Do some "heavy work"
  94. try {
  95. sleep(50); // Sleep for 50 milliseconds
  96. } catch (InterruptedException e) {}
  97. // Update the UI thread-safely
  98. UI.getCurrent().access(new Runnable() {
  99. @Override
  100. public void run() {
  101. progress.setValue(new Float(current));
  102. if (current < 1.0)
  103. status.setValue("" +
  104. ((int)(current*100)) + "% done");
  105. else
  106. status.setValue("all done");
  107. }
  108. });
  109. }
  110. // Show the "all done" for a while
  111. try {
  112. sleep(2000); // Sleep for 2 seconds
  113. } catch (InterruptedException e) {}
  114. // Update the UI thread-safely
  115. UI.getCurrent().access(new Runnable() {
  116. @Override
  117. public void run() {
  118. // Restore the state to initial
  119. progress.setValue(new Float(0.0));
  120. progress.setEnabled(false);
  121. // Stop polling
  122. UI.getCurrent().setPollInterval(-1);
  123. button.setEnabled(true);
  124. status.setValue("not running");
  125. }
  126. });
  127. }
  128. }
  129. // Clicking the button creates and runs a work thread
  130. button.addClickListener(new Button.ClickListener() {
  131. public void buttonClick(ClickEvent event) {
  132. final WorkThread thread = new WorkThread();
  133. thread.start();
  134. // Enable polling and set frequency to 0.5 seconds
  135. UI.getCurrent().setPollInterval(500);
  136. // Disable the button until the work is done
  137. progress.setEnabled(true);
  138. button.setEnabled(false);
  139. status.setValue("running...");
  140. }
  141. });
  142. ----
  143. The example is illustrated in <<figure.components.progressbar.thread>>.
  144. [[figure.components.progressbar.thread]]
  145. .Doing heavy work
  146. image::img/progressbar-thread.png[width=40%, scaledwidth=70%]
  147. endif::web[]
  148. [[components.progressbar.css]]
  149. == CSS Style Rules
  150. [source, css]
  151. ----
  152. .v-progressbar, v-progressbar-indeterminate {}
  153. .v-progressbar-wrapper {}
  154. .v-progressbar-indicator {}
  155. ----
  156. The progress bar has a [literal]#++v-progressbar++# base style.
  157. The progress is an element with [literal]#++v-progressbar-indicator++# style inside the wrapper, and therefore displayed on top of it.
  158. When the progress element grows, it covers more and more of the animated background.
  159. The progress bar can be animated (some themes use that).
  160. Animation is done in the element with the [literal]#v-progressbar-wrapper# style, by having an animated GIF as the background image.
  161. In the indeterminate mode, the top element also has the
  162. [literal]#++v-progressbar-indeterminate++# style.
  163. The built-in themes simply display the animated GIF in the top element and have the inner elements disabled.