我想在一个java.util.TimerTask 类里根据条件来改变界面类的LABLE的TEXT属性
我把改变LABLE的TEXT属性的方法放在构造函数里没问题,但放在RUN方法里却不行..求教高手..public void run() {
this.authenticate();
if (iAuthenticate != 1) {
readCardStatus.setStatusNum(92);//此处为改变LABLE的TEXT属性的方法,放在构造函数里没有错
}else{
readCardStatus.setStatusNum(100);//此处为改变LABLE的TEXT属性的方法,,放在构造函数里没有错
this.read();
String info = this.readfile();
System.out.println(info);
}
}报一下错误
Exception in thread "Timer-0" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:3777)
at org.eclipse.swt.SWT.error(SWT.java:3695)
at org.eclipse.swt.SWT.error(SWT.java:3666)
at org.eclipse.swt.widgets.Widget.error(Widget.java:463)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:355)
at org.eclipse.swt.widgets.Label.setText(Label.java:375)
at net.pyp.readCardClient.views.ReadCardView.changeStatus(ReadCardView.java:137)
at com.readCardSystem.read.ReadCardStatus.setStatusNum(ReadCardStatus.java:48)
at com.readCardSystem.read.ReadCardTask.run(ReadCardTask.java:76)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)是不是当Timer线程启动后他就不能改变界面程的控件属性?
我把改变LABLE的TEXT属性的方法放在构造函数里没问题,但放在RUN方法里却不行..求教高手..public void run() {
this.authenticate();
if (iAuthenticate != 1) {
readCardStatus.setStatusNum(92);//此处为改变LABLE的TEXT属性的方法,放在构造函数里没有错
}else{
readCardStatus.setStatusNum(100);//此处为改变LABLE的TEXT属性的方法,,放在构造函数里没有错
this.read();
String info = this.readfile();
System.out.println(info);
}
}报一下错误
Exception in thread "Timer-0" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:3777)
at org.eclipse.swt.SWT.error(SWT.java:3695)
at org.eclipse.swt.SWT.error(SWT.java:3666)
at org.eclipse.swt.widgets.Widget.error(Widget.java:463)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:355)
at org.eclipse.swt.widgets.Label.setText(Label.java:375)
at net.pyp.readCardClient.views.ReadCardView.changeStatus(ReadCardView.java:137)
at com.readCardSystem.read.ReadCardStatus.setStatusNum(ReadCardStatus.java:48)
at com.readCardSystem.read.ReadCardTask.run(ReadCardTask.java:76)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)是不是当Timer线程启动后他就不能改变界面程的控件属性?
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
Display.getCurrent().asyncExec(new Runnable() {
public void run() {
Button butt = new Button(panel,SWT.PUSH);
butt.setText("Push");
}
});方法syncExec()和asyncExec()的区别在于前者要在指定的线程执行结束后才返回,而后者则无论指定的线程是否执行都会立即返回到当前线程。