SWT - 显示繁忙的光标

时间:2012-09-17 20:10:14

标签: java cursor swt

我正在尝试在开始操作时更改光标。我希望光标显示忙,直到操作完成。我有一个单独的类用于operationListener。 我不知道如何分配光标?

从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()

2 个答案:

答案 0 :(得分:4)

您可以使用

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

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

答案 1 :(得分: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个实例。