SWT - 显示繁忙光标

5

我想在开始操作时更改鼠标指针,并希望鼠标指针一直显示繁忙状态,直到操作完成。我有一个专门处理操作监听器的类。但我不确定如何分配鼠标指针?

来自AplotBaseDialog类的调用

listOp.addOperationListener(new MyOperationListener(this) {
    etc....
} 

分离的类

 public abstract class MyOperationListener implements InterfaceAIFOperationListener {
   Cursor busyCursor = null;
   AplotBaseDialog w = null;

  public MyOperationListener(AplotBaseDialog win) {

  **Should this not be getCurrent?**
    busyCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT);

    w = win;
  } // end constructor

  public void startOperation(String startMessage) {
     ** should this be getting a Shell form the AplotBaseDialog? **
     w.???.setCursor(busyCursor);

  } // end startOperation()

  public void endOperation() {
     try {
        endOperationImpl();
     }
     finally {
        w.???.setCursor(null);
     }
  } // end endOperation()

  abstract protected void endOperationImpl();
 } // end class MyOperationListener

编辑

  plotButton.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent e) {
            BusyIndicator.showWhile(Display.getDefault(), new Runnable(){
               public void run(){
                  startPrinterListOperation(); 
               }
             });
         }
   });  

编辑

 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();
              }
           });
        }
        finally {

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


 } // end startPrinterListOperation()

最好使用Display.getSystemCursor(int)而不是自己创建光标对象,因为否则需要自己处理它们的释放。 - Marcono1234
2个回答

6

您可以使用

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

    public void run(){
       //your code
    }
    });

我尝试将其放入我的按钮操作中,但它没有任何作用。请参见上文。 - jkteater
你能发布一下你在startPrinterListOperation()中正在做什么吗?你看到任何异常吗? - sambi reddy
所以在操作完成后,您正在异步线程中打开对话框。 BusyIndicator会显示忙碌光标,直到可运行对象完成为止。我不确定*Operation类在您的情况下是如何工作的。您实际上可以将您正在执行的Operation代码放入Runnable.run()方法中,一旦完成,就像您之前所做的那样,在异步线程中打开对话框。 - sambi reddy

4
这对我有用:
private static boolean wait = false;

private static Cursor cursor = null;

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("Change cursor");

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

        @Override
        public void handleEvent(Event arg0) {
            wait = !wait;

            if(cursor != null)
                cursor.dispose();

            cursor = wait ? new Cursor(display, SWT.CURSOR_WAIT) : new Cursor(display, SWT.CURSOR_ARROW);

            shell.setCursor(cursor);
        }
    });


    shell.setSize(200,200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();

    if(cursor != null)
        cursor.dispose();
}

只需通过setCursor(Display, int)设置包含 shell 的光标即可。
如果您不想更改整个 shell 的光标,请使用您选择的 CompositeControl
请记住,您必须dispose()您自己创建的每个Cursor实例。

我基本上只是在我的操作方法中添加了上述行,因为它在对话框打开之前开始运行,看起来似乎有效。我真的希望我能够弄清楚如何使它在抽象操作类中工作。但这似乎有相同的效果。-谢谢 - jkteater

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接