使用BasicRobot时出现IllegalThreadStateException

时间:2011-07-28 21:45:30

标签: java multithreading exception fest

我在尝试使用BasicRobot(FEST)时得到以下内容。我不知道如何解决这个问题。

[ConditionalEventPump] Exception occurred during event dispatching:
java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
    at org.fest.swing.core.BasicRobot.waitForIdle(BasicRobot.java:669)
    at org.fest.swing.core.BasicRobot.waitForIdle(BasicRobot.java:654)
    at org.fest.swing.core.BasicRobot.click(BasicRobot.java:426)
    at org.fest.swing.core.BasicRobot.click(BasicRobot.java:387)
    at org.fest.swing.core.BasicRobot.click(BasicRobot.java:372)
    at org.fest.swing.core.BasicRobot.click(BasicRobot.java:360)
    at org.fest.swing.driver.ComponentDriver.click(ComponentDriver.java:94)
    at org.fest.swing.fixture.JButtonFixture.click(JButtonFixture.java:99)

以下是代码:

BasicRobot robot = (BasicRobot) BasicRobot.robotWithCurrentAwtHierarchy()

MainFrame.button( "setup" ).click();
//Exception is thrown in this line
DialogFixture setupViewDialog = WindowFinder.findDialog( "setup" ).using( robot );
setupViewDialog.button( "Save" ).click();

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

看起来你不能从EDT调用click(),所以开始一个新线程来调用它,如下所示:

BasicRobot robot = (BasicRobot) BasicRobot.robotWithCurrentAwtHierarchy()

new Thread(new Runnable() {
    @Override 
    public void run() {
        MainFrame.button("setup").click();
    } 
}).start();
DialogFixture setupViewDialog = WindowFinder.findDialog( "setup" ).using( robot );
new Thread(new Runnable() { 
    @Override
    public void run() {
        setupViewDialog.button("Save").click();
    } 
}).start();