线程和Swing
来源:互联网 发布:汉语网络域名注册 编辑:程序博客网 时间:2024/05/23 13:29
若需要完成一个耗时的任务,则可以启动一个新线程来处理。但是不能在新线程中直接操作swing组件!这可能导致swing崩溃。一种情况就是我们需要在更新界面的进度条或标签等。处理的办法是使用java.awt.EventQueue,将更新界面的代码放入static的invokeLater或invokeAndWait方法中。如更新一个标签[1]
EventQueue.invokeLater(new Runnable(){ public void run(){ lable.setText(percentage + "% complete"); } });
这样就将事件发布到事件队列中。没有新的线程被创建!
invokeLater方法直接返回,run()被异步执行。
invokeAndWait则直到run()执行结束返回You only need to use invokeLater
when you want to update your UI from another thread that is not the UI thread (event dispatch thread).
Suppose you have a handler for a button-click and you want to change the text of a label when someone clicks the button. Then it's perfectly save to set the label text directly. This is possible because the handler for the button-click event runs in the UI thread.
Suppose, however, that on another button-click you start another thread that does some work and after this work is finished, you want to update the UI. Then you useinvokeLater
. This method ensures that your UI update is executed on the UI thread.
So in a lot of cases, you do not need invokeLater
, you can simply do UI updates directly. If you're not sure, you can useisDispatchThread
to check whether your current code is running inside the event dispatch thread.
- 线程和Swing
- Swing线程--工作者线程和SwingWorker
- 浅析Swing线程模型和EDT
- 浅析Swing线程模型和EDT
- 浅析Swing线程模型和EDT
- swing线程
- Swing 线程
- Swing 线程
- 操作Swing线程:Swing核心
- 线程与Swing
- 使用Swing Worker线程
- 线程与Swing
- 线程与Swing
- 线程与Swing
- 线程与Swing
- Swing与线程
- swing包线程安全问题
- 论Java Swing线程
- 网络编程之TCP与UDP
- C#编程总结(六)异步编程
- 接口(interface)
- 从FusionIO的NVM压缩接口谈起
- [Network]网络层
- 线程和Swing
- sdk、jdk、jre的区别
- HDU 1575 Tr A
- 京东内部资料【自然搜索排序白皮书】打算混京东的屌丝必看!
- C#利用zxing.net生成二维码和条形码并实现打印的功能
- 一步步学习数据结构和算法之直接插入排序效率分析及java实现
- 黄河旋风继续上班
- table 动态添加行
- hudson 连 SVN