我做一个GUI的程序,基于eclipse swt,要10秒一次任务调度,要在GUI显示,可不可以这么用,是SWT线程模型导致的错误吗?
import org.eclipse.swt.widgets.Text;public class ClientFrame{
private Text text=new ....;
................
public void func(){
text.append("hello");
Timer timer=new Timer();
TimerTask task=new MornitorData(text);
timer.schedule(task,0,10000);
}
}other file:
public class MornitorData{
private Text text=null;
...................
public MornitorData(Text text){
this.text=text;
...........
} public void run(){
text.append("hello");//此处异常 exception in thread "Timer-0"
org.eclipse.swt.SWTException:Invalid thread access
}
}
import org.eclipse.swt.widgets.Text;public class ClientFrame{
private Text text=new ....;
................
public void func(){
text.append("hello");
Timer timer=new Timer();
TimerTask task=new MornitorData(text);
timer.schedule(task,0,10000);
}
}other file:
public class MornitorData{
private Text text=null;
...................
public MornitorData(Text text){
this.text=text;
...........
} public void run(){
text.append("hello");//此处异常 exception in thread "Timer-0"
org.eclipse.swt.SWTException:Invalid thread access
}
}
解决方案 »
- java wait()的一个问题
- 表达能力比较差啊,回复了将近200个帖子啊。没有一个人明白我的意思的啊。我重新又整理了一下,同时感谢大家前面的参与啊。真心谢谢大家啊!
- 如何查询数据库中 yyyy-mm-dd 提前 5 天的数据?
- 再问继承的函数调用问题
- 跪求!!JAVA迷茫
- 菜鸟问概念问题,朋友帮帮忙。我会很快结帖的。
- 大家讨论一下,Iterator遍历和List遍历有什么不同,在什么情况下,使用相应的遍历?
- 我的网站的字体制作,希望各位兄弟帮下忙?
- 读取excel文件的jdbc驱动是什么?
- 执行结果怎么跟我想的不一样?
- 字符串如何转换成Calendar
- 我的记事本选择了英文字体,然后在JTextArea里的中文全变成框框了,请问有什么办法解决
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;public class ProgressorBar {public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
ProgressBar bar = new ProgressBar (shell, SWT.SMOOTH);
bar.setBounds (10, 10, 200, 32);
shell.open ();
for (int i=0; i<=bar.getMaximum (); i++) {
try {Thread.sleep (100);} catch (Throwable th) {}
bar.setSelection (i);
}
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}