SWT - Grey out and disable current shell

笑着哭i 提交于 2019-12-11 00:52:48

问题


When I have a operation running in the back ground, I am setting my cursor to busy until the process completes. Is there a way to also grey out and disable the current Display/Dialog/Shell until the process completes. I want to visually let the user know that something is working and they have to wait.

EDIT

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
      }
  });

Method that runs a printer 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() (Seperate Class) opens a dialog with network printers, then with a button push sends a job to the selected printer. When that operation finishes the Plotter Dialog closes - This is the end of that method - baseDialog is the MAIN GUI

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

              }
           });
        }

回答1:


The following should do what you want. It will recursively disable and grey out all the Controls in your Shell. The Shell itself does not have a setGrayed method, but this will work:

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);
    }
}



回答2:


Use

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

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

It sets the busy cursor on all Shells (Window, Dialog, ...) for the current Display until the Runnable.run() is executed.




回答3:


Baz's answer was a great start for me, but doesn't act on Combo since it extends Composite. By making the call to setEnabled unconditional, every Control (including Combo) are enabled/disabled correctly.

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);
}


来源:https://stackoverflow.com/questions/12679450/swt-grey-out-and-disable-current-shell

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