Vaadin Web应用开发教程(24):UI组件-ProgressIndicator组件

jerry VaadinWeb 2015年11月25日 收藏

ProgressIndicator组件显示进程条,ProgressIndicator组件定时向服务器查询其当前值,如果值有变化,则更新进程条进度。Web应用本身无需实现任何查询服务器的操作,查询由ProgressIndicator组件自动完成。
ProgressIndicator组件的值域为0.0到1.0之间。缺省的查询(polling)间隔为1秒,可以使用setPollingInterval 修改。

  1. // Create the indicator
  2. final ProgressIndicator indicator =
  3. new ProgressIndicator(new Float(0.0));
  4. main.addComponent(indicator);
  5. // Set polling frequency to 0.5 seconds.
  6. indicator.setPollingInterval(500);

ProgressIndicator组件通常用来显示一些耗时操作(比如下载文件)的进度。 使用setValue 为ProgressIndicator组件设置当前值。
下面代码模拟一个费时操作提供ProgressIndicator组件显示进度。

  1. // Create an indicator that makes you look busy
  2. final ProgressIndicator indicator =
  3. new ProgressIndicator(new Float(0.0));
  4. main.addComponent(indicator);
  5.  
  6. // Set polling frequency to 0.5 seconds.
  7. indicator.setPollingInterval(500);
  8.  
  9. // Add a button to start working
  10. final Button button = new Button("Click to start");
  11. main.addComponent(button);
  12.  
  13. // Another thread to do some work
  14. class WorkThread extends Thread {
  15. public void run () {
  16. double current = 0.0;
  17. while (true) {
  18. // Do some "heavy work"
  19. try {
  20. sleep(50); // Sleep for 50 milliseconds
  21. } catch (InterruptedException e) {}
  22. // Show that you have made some progress:
  23. // grow the progress value until it reaches 1.0.
  24. current += 0.01;
  25. if (current>1.0)
  26. indicator.setValue(new Float(1.0));
  27. else
  28. indicator.setValue(new Float(current));
  29. // After all the "work" has been done for a while,
  30. // take a break.
  31. if (current > 1.2) {
  32. // Restore the state to initial.
  33. indicator.setValue(new Float(0.0));
  34. button.setVisible(true);
  35. break;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. // Clicking the button creates and runs a work thread
  42. button.addListener(new Button.ClickListener() {
  43. public void buttonClick(ClickEvent event) {
  44. final WorkThread thread = new WorkThread();
  45. thread.start();
  46. // The button hides until the work is done.
  47. button.setVisible(false);
  48. }
  49. });