Search code examples
javamultithreadingexceptionfest

IllegalThreadStateException while using BasicRobot


I'm getting the following while trying to use BasicRobot (FEST). I don't know how to fix this.

[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)

Here is the code:

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();

How can I fix this?


Solution

  • It looks like you can't call click() from the EDT, so start a new thread to call it, like so:

    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();