2014-03-31 95 views
0

我試圖用時間間隔在一頁上顯示一系列照片。在countinuos while循環,我得到:如何等待Vaadin循環?

while(true){ 

     if (zmienna == fa.length) zmienna = 0; 
     Image obrazek = new Image("",pliki[zmienna]); 

     layout.replaceComponent(staryObrazek, obrazek); 
     obrazek.requestRepaint(); 
     staryObrazek = obrazek; 
     zmienna++; 
     try { 
      Thread.sleep(2000) ; 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     } 

它僅顯示通知圖標,dispaying mwthod無環路正常工作。有誰知道我應該如何解決這個問題?

回答

1

在所有現代UI系統中,您將不得不暫停主線程,但使用後臺線程來更新UI。否則,你會阻止整個用戶界面。

在Vaadin 7中,您可以啓用服務器推送,然後使用後臺線程每2秒更改一次圖像。

啓用推入書中vaadin的描述https://vaadin.com/de/book/vaadin7/-/page/advanced.push.html

您的代碼看起來是這樣的:

public class PushyUI extends UI { 

@Override 
protected void init(VaadinRequest request) { 
    // Set first component/image 
    setContent(chart); 

    // Start the update thread 
    new ImgUpdThread().start(); 
} 

class ImgUpdThread extends Thread { 

    @Override 
    public void run() 
    {  
      // Update the data for a while 
      while (count < 100) { 
       Thread.sleep(1000); 

       access(new Runnable() { 
        @Override 
        public void run() { 
         //update the UI as in your code above 
        } 
       }); 
      } 
    } 

使用的接入(...)方法來同步訪問是非常重要的UI元素。