【问题标题】:SWT - Showing a busy CursorSWT - 显示一个忙碌的光标
【发布时间】:2012-09-17 20:10:13
【问题描述】:

我正在尝试在开始操作时更改光标。我希望光标显示忙碌,直到操作完成。我有一个单独的 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()

【问题讨论】:

  • 最好使用Display.getSystemCursor(int) 而不是自己创建游标对象,否则您需要自己处理它们。

标签: java swt mouse-cursor


【解决方案1】:

你可以使用

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

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

【讨论】:

  • 我尝试将它放在我的按钮操作中,但它没有做任何事情。请见上图
  • 你能在 startPrinterListOperation() 中发布你正在做的事情吗?你发现有什么例外吗?
  • 所以您在操作完成后在异步线程中打开一个对话框。 BusyIndi​​cator 显示忙碌光标,直到可运行对象完成。我不确定 ***Operation 类在您的情况下是如何工作的。您实际上可以将您正在执行的 **Operation 代码放在 Runnable.run() 方法中,一旦完成,就可以像上面那样在异步线程中打开对话框。
【解决方案2】:

这对我有用:

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 的光标,请使用您选择的 CompositeControl


请记住,您必须为自己创建的每个 Cursor 实例 dispose()

【讨论】:

  • 我基本上只是将以上几行添加到我的操作方法中,因为它在打开对话框之前就开始了,它似乎可以工作。我真希望我能弄清楚如何让它在抽象操作类中工作。但这似乎具有相同的效果。 - 谢谢
猜你喜欢
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 2019-01-20
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2012-11-09
相关资源
最近更新 更多