Threading issue in swing GUI with type()

Asked by Liam WHite

My code runs fine when executing just this method

http://pastebin.com/XPhZc0Zc

but when I try and call it from within a gui it falls over on s.type(...)

Any help would be appreciated!

[log] App.open C:/Program Files (x86)/Mozilla Firefox/firefox.exe(4664)
[log] CLICK on L(642,529)@S(0)[0,0 1920x1080]
java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
 at java.awt.Robot.checkNotDispatchThread(Unknown Source)
 at java.awt.Robot.waitForIdle(Unknown Source)
 at org.sikuli.script.RobotDesktop.mouseDown(RobotDesktop.java:76)
 at org.sikuli.script.Region._click(Region.java:2320)
 at org.sikuli.script.Region.click(Region.java:2198)
 at org.sikuli.script.Region.keyin(Region.java:2718)
 at org.sikuli.script.Region.type(Region.java:2678)
 at tradefunctions.main(tradefunctions.java:18)
 at GUI.actionPerformed(GUI.java:188)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
...

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

Sikuli's keyboard and mouse actions are internally implemented using java.awt.Robot and the used functions per definition cannot be run in code, that is dispatched from the Java Swing GUI dispatcher (EDT).

... which means it cannot be used in any methods that are added to GUI elements as event listeners.

The only solution is to delegate the call of the stuff containing the above Sikuli features to your own listener thread, that is triggered using notify() from the GUI element action function.

Revision history for this message
Liam WHite (liamawhite) said :
#3

Thanks RaiMan, that solved my question.