`

SWT界面Thread以外的线程如何控制界面变化

    博客分类:
  • Java
阅读更多

在SWT程序中,SWT会自动创建一个用户界面线程,非用户界面线程不能直接操作.

 用户界面线程。
因此经常抛出如下异常:

Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access

 

 

要想在另外一个线程中尝试修改用户界面,应采用以下方法:

 

1. 首先定义一个线程。例如

 

Runnable runnable = new Runnable() {
      public void run() {
          // 你改界面的代码
    }
};

 

2. 通过shell.getDisplay(),或直接引用display来将操作页面线程。

 

 

display.syncExec(runnable); 
或者
shell.getDisplay().syncExec(runnable); 

 

 

 

引用文档说明:

public void syncExec(java.lang.Runnable runnable)
Causes the run() method of the runnable to be invoked by the user-interface thread at the next reasonable opportunity. The thread which calls this method is suspended until the runnable completes.
Parameters:
runnable - code to run on the user-interface thread.

 

 

 

 
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics