Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

components-progressbar.asciidoc 6.6KB

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