由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - java update main UI from child thread issue (转载)
相关主题
Java 多线程 的架构如何改进?怎么练习multi-threading,平常工作都是用Java框架
写thread safe程序现在也是程序员必须要掌握的了吧重新学习Java Thread的Field变量与Thread Local
FMP supports UI thread in both Swing and SWTJava 多线程:还需要好CPU?
FMP 进驻 Programming 版C# thread
问题一枚图形界面编程的问题
用java写的用户界面速度慢,用户体验很差.为什么还这么多人用java写GUI的软件?C#处理XML的问题
Q: 2 submit buttons in 1 page (转载)请问text版linux怎样加装xwindows/GUI
java concurrency时,你们用的callable还是runnable?请问有人使用wxpython出现显示问题吗?
相关话题的讨论汇总
话题: text话题: button话题: shell话题: new
进入Programming版参与讨论
1 (共1页)
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!
1 (共1页)
进入Programming版参与讨论
相关主题
请问有人使用wxpython出现显示问题吗?问题一枚
真正的multi-threading是5个thread要5个cpu?那apache是真正的m用java写的用户界面速度慢,用户体验很差.为什么还这么多人用java写GUI的软件?
FMP 一个完整可运行的范例程序Q: 2 submit buttons in 1 page (转载)
FMP mini profilejava concurrency时,你们用的callable还是runnable?
Java 多线程 的架构如何改进?怎么练习multi-threading,平常工作都是用Java框架
写thread safe程序现在也是程序员必须要掌握的了吧重新学习Java Thread的Field变量与Thread Local
FMP supports UI thread in both Swing and SWTJava 多线程:还需要好CPU?
FMP 进驻 Programming 版C# thread
相关话题的讨论汇总
话题: text话题: button话题: shell话题: new