ProgressMessageBox未更新

时间:2014-06-11 07:11:13

标签: gwt gxt

在GXT(来自Sencha)中,我试图使用ProgressMessageBox向用户显示工作正在完成,但在完成所有工作之后,我才看到消息框。这是代码:

final ProgressMessageBox messageBox = new ProgressMessageBox("Task Description",
                                                             "Executing Task...");
messageBox.setProgressText("Calculating...");
messageBox.setPredefinedButtons();
messageBox.show();
for (int i = 0; i < 5; ++i) {
  for (long l = 0l; l < 10000000000l; ++l) {
    if (l == 12345l) {
      MyUtil.info(60, "" + System.currentTimeMillis() / 1000);
    }
  }
  messageBox.updateProgress((double)(i + 1) / 5, "{0}% Complete");
}

此代码位于菜单项的SelectionHandler中。显然,我实际上并不只是在“真实”代码中循环几十亿次,但是当我执行我想要执行的工作时,我得到相同的结果......在完成所有工作后显示消息框。我看到“info”消息(MyUtil#info只是一个围绕GXT Info功能的包装器,它会导致信息消息显示指定的秒数)...并且在我的机器上,运行显示的代码,每条消息的值比前一条消息大7秒(但它们都与消息框同时显示)。

在调用ProgressMessageBox#updateProgress强制屏幕或消息框刷新后,我需要做些什么吗?

1 个答案:

答案 0 :(得分:0)

我可以通过将任务放入Scheduler#execute方法来实现此目的。 Sencha Explorer Demo中的示例使用Timer,但由于我不知道执行需要多长时间,因此我选择使用Scheduler方法。以下是相关的最终代码:

...
menuItem.addSelectionHandler(new SelectionHandler<Item>() {
  @Override
  public void onSelection(final SelectionEvent<Item> selectionEvent) {
    final ProgressMessageBox messageBox = new ProgressMessageBox("Size All Columns", //
                                                                 "Resizing Columns...");
    messageBox.setProgressText("Calculating...");
    //  messageBox.setPredefinedButtons();
    messageBox.show();
    resizeNextColumn(messageBox,
                     _selectionModel instanceof CheckBoxSelectionModel ? 1 : 0,
                     _grid.getColumnModel().getColumnCount() - 1);
  }
});
...
private void resizeNextColumn(final ProgressMessageBox messageBox,
                              final int columnIndex,
                              final int lastColumnIndex) {
  Scheduler.get().scheduleDeferred(new ScheduledCommand() {
    @Override
    public void execute() {
      resizeColumnToFit(columnIndex);
      if (columnIndex == lastColumnIndex) {
        messageBox.hide();
        _grid.getView().refresh(true);
        return;
      }
      messageBox.updateProgress((double)columnIndex / (lastColumnIndex + 1),
                                "{0}% Complete");
      resizeNextColumn(messageBox, columnIndex + 1, lastColumnIndex);
    }
  });
}

有一件似乎不起作用的事情是messageBox.setPredefinedButtons()调用...当我使用它时,消息框中的进度条根本不会更新。我真的不想要一个&#34; OK&#34;对话框上的按钮,但现在它必须这样做。我正在使用3.1.0的GXT。