Java SWT: How to make a button unclickable while a separate thread is running(Java SWT:如何在单独的线程运行时使按钮不可点击)
本文介绍了Java SWT:如何在单独的线程运行时使按钮不可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个程序,其中pushbutton
上的SelectionEvent
侦听器激活一个线程。
在此线程的执行期间,该按钮需要保持灰色且不可单击(以便不运行相关线程的多个副本),然后返回到正常的可单击状态。
在SWT框架中,我如何才能做到这一点?
这是我目前拥有的代码(‘ok’是有问题的SWT按钮):
ok.addSelectionListener(new SelectionListener() {
public void greyOut() {
ok.setEnabled(false);
}
public void ungray() {
ok.setEnabled(false);
}
public void widgetDefaultSelected(SelectionEvent e) {}
public void widgetSelected(SelectionEvent ef) {
ok.setGrayed(true);
Thread t = new Thread(new Runnable() {
public void run() {
greyOut();
connectAndDownloadReport();
try {
System.setOut(oldOut);
ArrayList<Devices> allDevs = parseFile();
createCSV(allDevs);
}
catch(Exception e) {
e.printStackTrace();
e.getMessage();
System.out.close();
System.exit(0);
}
finally {
ungray();
}
}
});
}});
推荐答案
调用ok.setEnabled(false)
将禁用该按钮,但您必须在UI线程中执行此操作。因此,在后台线程中,您必须使用Display.asyncExec
来运行该部分代码:
Display.getDefault().asyncExec(() -> ok.setEnabled(false));
这篇关于Java SWT:如何在单独的线程运行时使按钮不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:Java SWT:如何在单独的线程运行时使按钮不可点击


基础教程推荐
猜你喜欢
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01