2

当我在后台运行一个操作时,我将光标设置为忙,直到该过程完成。有没有办法让当前的 Display/Dialog/Shell 变灰并禁用,直到该过程完成。我想直观地让用户知道某些东西正在工作,他们必须等待。

编辑

plotButton.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event arg0) {
         getShell().setEnabled(!getShell().getEnabled());
         getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT));    
         recursiveSetEnabled(getShell(), getShell().getEnabled());
         startPrinterListOperation(); <== This is method that runs operation
      }
  });

运行打印机操作的方法。

private void startPrinterListOperation() {
  listOp = new AplotPrinterListOperation(appReg.getString("aplot.message.GETPRINTERLIST"), session);
  listOp.addOperationListener(new MyOperationListener(this) {
     public void endOperationImpl() {
        try {
           printers.clear();
           printers.addAll((ArrayList<PrinterProfile>) listOp.getPrinters());
           Display.getDefault().asyncExec(new Runnable() {
              public void run() {
                showAplotPlotterDialog(); <== When operation returns - opens selection dialog
              }
           });
        }
        finally {

           listOp.removeOperationListener(this);
           listOp = null;
        }
     }
  });
  session.queueOperation(listOp);
} // end startPrinterListOperation()

showAplotPlotterDialog()(单独的类)打开一个与网络打印机的对话框,然后按下按钮将作业发送到选定的打印机。当该操作完成时,绘图仪对话框关闭 - 这是该方法的结束 - baseDialog 是主 GUI

finally {
           plotOp.removeOperationListener(this);
           plotOp = null;
           Display.getDefault().asyncExec(new Runnable() {
              public void run() {
                baseDialog.removeAllTableRows();
                baseDialog.plotRequestCompleted = true;
                baseDialog.setResultsButtonVisibility();
                getShell().close();

              }
           });
        }
4

3 回答 3

2

以下应该做你想要的。它将递归地禁用并灰显Control您的Shell. Shell本身没有方法,setGrayed但这会起作用:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");

    button.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            shell.setEnabled(!shell.getEnabled());
            shell.setCursor(new Cursor(display, SWT.CURSOR_WAIT));      
            recursiveSetEnabled(shell, shell.getEnabled());
        }
    });

    new Text(shell, SWT.NONE).setText("TEXT");

    shell.setSize(400, 400);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

private static void recursiveSetEnabled(Control control, boolean enabled) {
    if (control instanceof Composite)
    {
        Composite comp = (Composite) control;

        for (Control c : comp.getChildren())
            recursiveSetEnabled(c, enabled);
    }
    else
    {
        control.setEnabled(enabled);
    }
}
于 2012-10-02T07:31:43.470 回答
1

采用

BusyIndicator.showWhile(Display.getDefault(), new Runnable()
{

    public void run()
    {
        //operation
    }
});

Shell它在当前的所有s ( Window, Dialog, ...)上设置忙光标,Display直到Runnable.run()执行。

于 2012-10-01T22:58:05.550 回答
0

ComboBaz 的回答对我来说是一个很好的开始,但由于它扩展而没有采取行动Composite。通过setEnabled无条件调用,每个Control(包括Combo)都被正确启用/禁用。

private static void recursiveSetEnabled(Control control, boolean enabled) {
    if (control instanceof Composite)
    {
        Composite comp = (Composite) control;

        for (Control c : comp.getChildren())
            recursiveSetEnabled(c, enabled);
    }

    control.setEnabled(enabled);
}
于 2016-10-17T09:57:50.900 回答