2012-09-17 14 views
5

Tôi đang cố gắng thay đổi con trỏ khi tôi bắt đầu một thao tác. Tôi muốn con trỏ hiển thị bận cho đến khi thao tác kết thúc. Tôi có một lớp riêng biệt cho activityListener. Tôi không chắc chắn cách gán con trỏ?SWT - Hiển thị Con trỏ bận rộn

Gọi từ lớp AplotBaseDialog

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

lớp riêng biệt

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 

EDIT

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

EDIT

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

Trả lời

4

này làm việc cho tôi:

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

Chỉ cần đặt con trỏ của vỏ chứa của bạn thông qua setCursor(Display, int).

Nếu bạn không muốn thay đổi con trỏ cho toàn bộ hệ vỏ, hãy sử dụng số Composite hoặc Control do bạn chọn.


Hãy nhớ rằng bạn phải tự chụp dispose() mỗi trường hợp bạn tự tạo.

+0

Tôi về cơ bản chỉ cần thêm các dòng trên vào phương thức hoạt động của tôi khi nó bắt đầu sau đó ngay trước khi hộp thoại được mở và có vẻ như nó hoạt động. Tôi thực sự muốn tôi có thể đã tìm ra cách làm cho nó hoạt động trong lớp hoạt động trừu tượng. Nhưng điều này dường như có cùng tác dụng. - Cảm ơn – jkteater

4

Bạn có thể sử dụng

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

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

Tôi đã cố gắng đặt điều đó vào hành động nút của mình, nhưng nó không làm gì cả. Xin vui lòng xem trên – jkteater

+0

bạn có thể đăng những gì bạn đang làm trong startPrinterListOperation()? Bạn có thấy ngoại lệ nào không? –

+0

Không có ngoại lệ - xem mã ở trên – jkteater