ERROR: Cannot call method from the event dispatcher thread

Asked by Antoni Illana on 2017-04-11

Hello

I have a serious problem, I am writing code in Java with Eclipse to enter information in a web portal. But I get the ERROR: Can not call method from the dispatcher thread event.

I tried to add a thread to avoid the problem, but in this case pe will appear another [error] mouseDown: buttons still pressed - using all
Java.lang.IllegalThreadStateException: Can not call method from the dispatcher thread event

In the User entry, the cursor is positioned in the field, but does not write anything.
In the button click, the cursor is positioned and clicked, but does not perform the action.

I can not find a solution for more that I have looked for, you can help me.

This is my code
public class Main {

 public static void main(String[] args) {

  Fn.WriteLog(" Usuario");
  error = Fn.TypeByImage(driver, SeleniumImages+"Usuario.png", "tu1");
  if (error) {
   errorText = "Error: Usuario";
   return errorText;
  }

  Fn.WriteLog(" Iniciar Sesión");
  //error = Fn.TypeByImage(driver, SeleniumImages+"BotonIniciarSesion.PNG", Key.ENTER);

  // TEST
  new ThreadExperiment();

 }

}

 public static boolean TypeByImage(WebDriver driver, String Imagen, String Type){
  boolean error = false;

  try{

   Screen screen = new Screen();
   Pattern imagen = new Pattern(Imagen).similar(0);

   screen.type(imagen, Type);

         try {
          Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }

  }catch(Exception err){
   err.printStackTrace();
   Fn.WriteLog("ERROR: " + err.getMessage());
  }

  return error;
 }

 public static boolean ClickByImage(String Imagen){
  boolean error = false;

  Screen screen = new Screen();
  Pattern imagen = new Pattern(Imagen).similar(0);

  try{
         try {
     screen.wait(imagen,100);
    } catch (FindFailed e2) {
     e2.printStackTrace();
    }

          try {
     screen.click(imagen);
    } catch (FindFailed e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }

          try {
           Thread.sleep(1000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }

  }catch(Exception err){
   err.printStackTrace();
   Fn.WriteLog("ERROR: " + err.getMessage());
  }

  return error;
 }

import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

public class ThreadTest implements Runnable {

    Thread t;

    ThreadTest() {

       // thread created
       t = new Thread(this, "Admin Thread");

       // prints thread created
       System.out.println("thread = " + t);

       // this will call run() function
       t.start();
    }

    @Override
    public void run() {

       System.out.println("Inside run()function");
       Screen screen = new Screen();
   Pattern imagen = new Pattern("//Selenium/Images/BotonIniciarSesion.PNG").similar(0);
             try {
        screen.wait(imagen, 5);
        Fn.dormir(1);
        screen.click();
                 Thread.sleep(1000);

             } catch (InterruptedException | FindFailed e){
              e.printStackTrace();
              System.out.println("Error");
             }

    }

 }

--ERROR DEBUG--

   Usuario
[debug] Screen: initScreens: basic initialization (1 Screen(s) found)
[debug] *** monitor configuration (primary: 0) ***
[debug] Screen 0: S(0)[0,0 1680x1050]
[debug] *** end monitor configuration ***
[debug] waiting for P(//Portable Eclipse/Selenium/Images/Usuario.png) S: 0.0 to appear
[debug] Screen.capture: java.awt.Rectangle[x=0,y=0,width=1680,height=1050]
[debug] RobotDesktop: captureScreen: on 0 using java.awt.Rectangle[x=0,y=0,width=1680,height=1050]
[debug] ResourceLoaderBasic: SikuliX Package Build: 1.0.1 12SEP2013160242
[debug] ResourceLoaderBasic: check: we are running on arch: x86
[debug] ResourceLoaderBasic: check: using Java at: D:/USR/LOCAL/java/jre17002b13/
[debug] ResourceLoaderBasic: check: Exists libs folder at location of jar? YES: G:/Portable Eclipse/Sikuli/
[debug] ResourceLoaderBasic: checkLibsDir: G:/Portable Eclipse/Sikuli/libs
[debug] ResourceLoaderBasic: loadLib: WinUtil
[debug] ResourceLoaderBasic: loadLib: Found: WinUtil
[debug] ResourceLoaderBasic: loadLib: Now loaded: WinUtil
[debug] ResourceLoaderBasic: checkLibsDir: Using libs at: G:\Portable Eclipse\Sikuli\libs
[debug] ResourceLoaderBasic: check: Using this as OCR directory (tessdata) too
[debug] ResourceLoaderBasic: loadLib: VisionProxy
[debug] ResourceLoaderBasic: loadLib: Found: VisionProxy
[debug] ResourceLoaderBasic: loadLib: Now loaded: VisionProxy
[debug] P(//Portable Eclipse/Selenium/Images/Usuario.png) S: 0.0 has appeared.
[log] CLICK on L(850,239)@S(0)[0,0 1680x1050]
java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
ERROR: 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 Clarity.Fn.TypeByImage(Fn.java:139)
 at Clarity.Bot_Carga_DWH.Introducir_usuario(Bot_Carga_DWH.java:82)
 at Clarity.Carga_Excel.carga_excel(Carga_Excel.java:157)
 at Clarity.Ventana$4.actionPerformed(Ventana.java:183)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
 at java.awt.EventQueue.access$000(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)

   Iniciar Sesión
thread = Thread[Admin Thread,6,main]
Inside run()function
[debug] waiting for P(//Portable Eclipse/Selenium/Images/BotonIniciarSesion.PNG) S: 0.0 to appear
[debug] Screen.capture: java.awt.Rectangle[x=0,y=0,width=1680,height=1050]
[debug] RobotDesktop: captureScreen: on 0 using java.awt.Rectangle[x=0,y=0,width=1680,height=1050]
[debug] P(//Portable Eclipse/Selenium/Images/BotonIniciarSesion.PNG) S: 0.0 has appeared.
[log] CLICK on L(773,331)@S(0)[0,0 1680x1050]
[error] mouseDown: buttons still pressed - using all

Question information

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2017-04-11
Last reply:
2017-04-11
RaiMan (raimund-hocke) said : #1

Might be, that this is your code, but not all:
You seem to use some Java AWT components, since there is this:
....
 at Clarity.Fn.TypeByImage(Fn.java:139)
 at Clarity.Bot_Carga_DWH.Introducir_usuario(Bot_Carga_DWH.java:82)
 at Clarity.Carga_Excel.carga_excel(Carga_Excel.java:157)
 at Clarity.Ventana$4.actionPerformed(Ventana.java:183)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 ....
which I cannot see in your code.

This problem has nothing to do with the Sikuli features.
Sikuli internally uses Java Robot for mouse, keyboard and screen capture, which cannot be used in Swing/AWT event handlers by Java design.

the solution:
put everything that uses Sikuli features in a thread and run this from the event handler (button pressed in this case).

Can you help with this problem?

Provide an answer of your own, or ask Antoni Illana for more information if necessary.

To post a message you must log in.