java SWT - 显示繁忙的光标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12466393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SWT - Showing a busy Cursor
提问by jkteater
I am trying to change the cursor when I start a operation. I want the cursor showing busy until the operation is finished. I have a separate class for the operationListener. I am not sure how to assign the cursor?
我试图在开始操作时更改光标。我希望光标显示忙碌,直到操作完成。我有一个单独的类用于 operationListener。我不确定如何分配光标?
Call from AplotBaseDialog class
从 AplotBaseDialog 类调用
listOp.addOperationListener(new MyOperationListener(this) {
etc....
}
Separate Class
分班
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()
采纳答案by Baz
This works for me:
这对我有用:
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();
}
Just set the cursor of your containing shell via setCursor(Display, int)
.
If you don't want to change the cursor for the whole shell, use a Composite
or Control
of your choice.
如果您不想更改整个 shell 的光标,请使用您选择的Composite
或Control
。
Remember that you have to dispose()
each Cursor
instance you create yourself.
请记住,您必须自己创建dispose()
每个Cursor
实例。
回答by sambi reddy
You could use
你可以用
BusyIndicator.showWhile(Display.getDefault(), new Runnable(){
public void run(){
//your code
}
});