How to use “Scheduler.get().scheduleDeferred” the right way in GWT?

[亡魂溺海] 提交于 2019-12-23 01:37:11

问题


Here is my Pseudocode in my GWT app.

-Visible the loading Label
-Loading text from properties file (may take long)
-Invisible the loading Label & Visible the main HTMLPanel

So I want to use Scheduler.get().scheduleDeferred to achive that, here is the code:

loadingLabel.setVisible(true);
Scheduler.get().scheduleDeferred(new ScheduledCommand() { 
  @Override
  public void execute() {
   loadingText();
  }
}
loadingLabel.setVisible(false);
mainHTMLPanel.setVisible(true);

But it doesn't work correctly as it did not show the loadingLabel but show the mainHTMLPanel immediately & when i click a textbox inside the mainHTMLPanel since the Gui got frozen cos it is loading text. Then I have to wait for a while to be able to click the textbox inside mainHTMLPanel.

But if i put loadingLabel.setVisible(false); & mainHTMLPanel.setVisible(true); inside execute() Then it works.

But i am not sure that is the right way to do.

So, is the following code the right way to use Scheduler.get().scheduleDeferred?

    loadingLabel.setVisible(true);
    Scheduler.get().scheduleDeferred(new ScheduledCommand() { 
      @Override
      public void execute() {
       loadingText();
       loadingLabel.setVisible(false);
       mainHTMLPanel.setVisible(true);
      }
    }

回答1:


Use a scheduler with "fixed delay" that will check each 100 milliseconds that the text content is loaded. Once the text is loaded hide your "loadingLabel" and display the "mainHTMLPanel".

mainHTMLPanel.setVisible(false);
loadingLabel.setVisible(true);

Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {
   @Override
   public boolean execute() {
       if (isTextLoaded())  {
            loadingLabel.setVisible(false);
            mainHTMLPanel.setVisible(true);
            return false;
       }
       else {return true;}
    }
}, 100);

private boolean isTextLoaded() {
    // Dummy check
    MyWidgetContainingText.getContent().isNotEmpty();
}


来源:https://stackoverflow.com/questions/19647905/how-to-use-scheduler-get-scheduledeferred-the-right-way-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!