← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #250327]: How to use Java Swing in Sikuli scripts - solution: use javax.swing.SwingWorker

 

Question #250327 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/250327

Description changed to:
**** problem
since Sikuli itself uses java.awt features, scripts cannot simply implement and use Swing elements the normal way.

**** solution
see: https://wiki.python.org/jython/SwingWorker

--------------------------------------------------------

This is barely modified example from https://wiki.python.org/jython/SwingExamples#JButton_and_Button_events
Main.py file

from javax.swing import JFrame
from javax.swing import JLabel
from javax.swing import JButton
from java.awt import BorderLayout
from sikuli.Sikuli import *

rest = True

class Window:
  def setText(self,event):
      wait(2)
     click("1402946292490.png")

  def __init__(self):
    frame = JFrame("Jython Example JButton")
    frame.setSize(100, 100)
    frame.setLayout(BorderLayout())
    self.label = JLabel('Hi ')
    frame.add(self.label, BorderLayout.NORTH)
    button = JButton('Click Me',actionPerformed=self.setText)
    frame.add(button, BorderLayout.SOUTH)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    frame.setVisible(True)

if __name__ == '__main__':
     Window()

in result, when i press button, sikuli is clicking correctly but mouse
button stays pressed

log is:
[log] CLICK on L(399,880)@S(0)[0,0 1600x900]

Exception in thread "AWT-EventQueue-0" 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.click(Region.java:2181)
... more stack trace

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.