Created
June 5, 2012 21:04
-
-
Save remram44/2877919 to your computer and use it in GitHub Desktop.
Swing from Jython - threading issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import threading | |
| from javax.swing import JFrame, JButton | |
| from java.awt.event import ActionListener, WindowAdapter | |
| class Frame(JFrame, ActionListener): | |
| def __init__(self): | |
| print "constructing! thread is %s" % threading.currentThread() | |
| button = JButton("Hello") | |
| button.setActionCommand('button') | |
| button.addActionListener(self) | |
| self.getContentPane().add(button) | |
| self.pack() | |
| self.setResizable(False) | |
| # @Override | |
| def actionPerformed(self, event): | |
| print "event! thread is %s" % threading.currentThread() | |
| endCondition = threading.Condition() | |
| class Adapter(WindowAdapter): | |
| # @Override | |
| def windowClosing(self, event): | |
| endCondition.acquire() | |
| endCondition.notifyAll() | |
| endCondition.release() | |
| if __name__ == '__main__': | |
| endCondition.acquire() | |
| frame = Frame() | |
| frame.setVisible(True) | |
| frame.addWindowListener(Adapter()) | |
| # We don't use setDefaultCloseOperation(EXIT_ON_CLOSE), which would call | |
| # Java's System.exit() instead of Python's sys.exit() | |
| endCondition.wait() | |
| endCondition.release() | |
| print 'sys.exit()' | |
| sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment