首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更新textfield时无效的线程访问异常

更新textfield时无效的线程访问异常
EN

Stack Overflow用户
提问于 2015-03-12 22:39:06
回答 1查看 206关注 0票数 0

我试图使用可运行的线程更新属于另一个类的textfield,但是我得到了“无效线程访问”异常。

我的代码是:

代码语言:javascript
运行
复制
 Thread t2 = new Thread () {

    public void run () {
        System.out.println("t2 thread içindeyim.");
        try {
            String sql =  " RESTORE DATABASE Genius3"+
                          " FROM DISK = '"+collected.getdbpath()+"'"+
                          " WITH MOVE 'GeniusIII_Data' TO 'C:\\SQLDATA\\Genius3.mdf',"+
                          " MOVE 'GeniusIII_Log' TO 'C:\\SQLDATA\\Genius3_1.ldf'";

            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url);
            stmt = con.prepareStatement(sql);
            rs = stmt.executeQuery();}
            catch (Exception ef) {ef.printStackTrace();}
            finally {

           ekran5.text_1.setText("done");

           }        
    }
};

还有另一个类,它里面有那个文本字段;

代码语言:javascript
运行
复制
 public class ekran5 {

public static Label islemlabel;

public static Composite composite_1 ;
public static Label detail;
 public static  Text text_1;
public static void start(){


composite_1 = new Composite(Loader.composite, SWT.BORDER | SWT.EMBEDDED);
composite_1.setBackground(SWTResourceManager.getColor(192,192,192));
composite_1.setBounds(362, 83, 668, 536);

islemlabel = new Label(composite_1, SWT.NONE);
islemlabel.setFont(SWTResourceManager.getFont("Arial CYR", 10, SWT.NORMAL));
islemlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
islemlabel.setBounds(10, 448, 84, 15);
islemlabel.setText("\u0130\u015Flem Detay\u0131");

text_1 = new Text(composite_1, SWT.BORDER);
text_1.setBounds(10, 469, 628, 37);
}

}

在“最后”块中,我正在更新textfield,但由于这个原因,它给了我一个无效的线程访问异常。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-13 04:50:02

在SWT中,只允许UI线程更新小部件,尝试使用Display.syncExec执行所有UI更新。(或者,可以使用UI作业。)

此外,阅读SWT常见问题将是一个明智的举动,这听起来像一个常见问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29021634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档