c*******9 发帖数: 6411 | 1 【 以下文字转载自 Java 讨论区 】
发信人: cplus2009 (in the woods (木老虎)), 信区: Java
标 题: update main UI from child thread issue
发信站: BBS 未名空间站 (Tue May 21 14:13:10 2013, 美东)
I tried the following code, and looks like text.setText("test") caused
the "org.eclipse.swt.SWTException: Invalid thread access" error.
the UI here has text field, and a browser button, the browser button will
display JFileChooser to choose a file.
any idea how to get this work?
thanks.
testing code:
private void createContents(final Shell shell)
shell.setLayout(new GridLayout(3, false));
final Label lable = new Label(shell, SWT.READ_ONLY);
lable.setText("File Path");
final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text .setBounds(100, 75, 100, 20);
text .setTextLimit(150);
Button button = new Button(shell, SWT.PUSH);
button.setText("Browse...");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Display.getDefault().asyncExec(new Runnable() {
public void run()
{
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setMultiSelectionEnabled(false);
int status = jfc.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION)
{
File browseFile = jfc.getSelectedFile();
text.setText("test");
}
}
});
}
});
| w******p 发帖数: 166 | 2 yo should use SwingUtilities.invokeLater and let GUI thread update all GUI
stuff | c*******9 发帖数: 6411 | 3 thanks.
i changed Display.getDefault().asyncExec to SwingUtilities.invokeLater but
get the same.
could you elaborate on how i can update text.setText("test"), the GUI thread
is the main thread,right? how can I get back to the main thread and do the
update properly inside the widgetSelected function. | o**2 发帖数: 168 | 4 在Swing里,UI thread是固定的,所以可以用SwingUtilities.invokeLater()。
但在SWT里,UI thread不是固定的,所以必须用display.syncExec()。
http://www.eclipse.org/swt/faq.php#uithread
虽然这些thread-based的编程技术还在使用,但会被新的编程技术淘汰掉的,比如我开
发的FMP。 | c*******9 发帖数: 6411 | 5 thank you, that works!
【在 o**2 的大作中提到】 : 在Swing里,UI thread是固定的,所以可以用SwingUtilities.invokeLater()。 : 但在SWT里,UI thread不是固定的,所以必须用display.syncExec()。 : http://www.eclipse.org/swt/faq.php#uithread : 虽然这些thread-based的编程技术还在使用,但会被新的编程技术淘汰掉的,比如我开 : 发的FMP。
| o**2 发帖数: 168 | 6 cplus2009 同学,我另开了一个帖子,介绍如何用FMP在做这种线程之间的转换。
http://www.mitbbs.com/article_t/Programming/31254349.html
【在 c*******9 的大作中提到】 : thank you, that works!
|
|