Swing's Event Dispatch Thread (EDT)
来源:互联网 发布:动艺网络 编辑:程序博客网 时间:2024/04/29 23:57
Swing's Event Dispatch Thread (EDT)
Accessing GUI components in the EDT
The cardinal rule of Swing threading states that all access to all Swing components should be done in the event dispatch thread (EDT.) To avoid unexpected behavior in your GUI tests (e.g. random test failures,) this rule also applies to test code.
FEST-Swing performs all access to Swing components in the EDT. FEST-Swing also exposes a convenient mechanism to access Swing components in the EDT from your test code. This mechanism involves three classes:
GuiQuery
, for reading property values from GUI componentsGuiTask
, for performing actions on GUI componentsGuiActionRunner
, executes aGuiQuery
orGuiTask
in the EDT, re-throwing any exceptions thrown when executing any GUI action in the EDT.
Examples:
The following examples demonstrate how to access GUI components in the EDT using FEST-Swing.
1. Reading the text of a JLabel
:
final JLabel nameLabel = ... // get a reference to a JLabel String text = GuiActionRunner.execute(new GuiQuery<String>() { public String executeInEDT() { return nameLabel.getText(); } });
2. Setting the text of a JLabel
:
final JLabel nameLabel = ... // get a reference to a JLabel GuiActionRunner.execute(new GuiTask() { public void executeInEDT() { nameLabel.setText("Name:"); } });
Testing that access to GUI components is done in the EDT
FEST provides the class FailOnThreadViolationRepaintManager
that fails if access to GUI components is not performed on the EDT. This RepaintManager
on based on ThreadCheckingRepaintManager
by Scott Delap and Alex Potochkin. For more details about how to check if GUI component is done outside the EDT, please check Alex Potochkin's excellent article "Debugging Swing, the final summary."
Installing FailOnThreadViolationRepaintManager
is pretty simple. The following example shows how to install it in the class-level setup method of a TestNG test:
@BeforeClass public void setUpOnce() { FailOnThreadViolationRepaintManager.install(); }
When using Sun's JVM, a new instance of FailOnThreadViolationRepaintManager
will be set as the default repaint manager in Swing once and only once, regardless of the number of times install
is called. On other JVMs this optimization is not guaranteed.
Once a FailOnThreadViolationRepaintManager
is installed, it will throw a EdtViolationException
if access to GUI components is not performed in the EDT when executing a FEST test.
- Swing's Event Dispatch Thread (EDT)
- Swing 中换皮肤 解决Component creation must be done on Event Dispatch Thread
- Java swing使用皮肤后出现Component creation must be done on Event Dispatch Thread错误解决方法
- main and the GUI Event Dispatch Thread
- main and the GUI Event Dispatch Thread
- Swing之EDT简析
- Java多线程之"事件分发线程"----Event Dispatch Thread
- Component creation must be done on Event Dispatch Thread
- Java多线程之"事件分发线程"----Event Dispatch Thread
- SWING 和 LWUIT为什么要使用EDT
- 浅析Swing线程模型和EDT
- 深入浅出Swing事件分发线程(EDT线程)
- 浅析Swing线程模型和EDT
- Java Swing 常识篇之EDT
- 浅析Swing线程模型和EDT
- org.pushingpixels.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread
- 报错:”Gradle error : Write access is allowed from event dispatch thread only in android studio
- android studio 报Write access is allowed from event dispatch thread only
- Talisman Desktop
- 在Ubuntu上用gfceu玩FC游戏
- 《HP大中华区总裁孙振耀退休感言》
- java技术发展的一些历史
- python_0_challenge_ans
- Swing's Event Dispatch Thread (EDT)
- XFire中获取客户端IP
- 棉试题
- 判断浏览器类型
- 公司网站数据库挂马临时处理
- Xp 加入域时无法联系域控制器的解决
- 设计模式之Factory
- 安装 Ruby On Rails on Windows
- jQuery实现的显示 隐藏效果