O'Reilly logo

Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython by Richard Hightower

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java Event Handling

Here's an example of Java event handling in Jython. It does the same thing the example in the previous section did but in the Java (Jython) way.

>>> from javax.swing import JFrame, JButton
>>> from java.awt.event import ActionListener
>>> class hello(ActionListener):
...   def actionPerformed(self, event):
...           print "Hello"
...
>>> f = JFrame()
>>> b = JButton("Hello from Java Events")
>>> hi = hello()
>>> b.addActionListener(hi)
>>> f.getContentPane().add(b)
>>> f.pack()
>>> f.visible = 1

I think you'll prefer the more Pythonesque way of doing things because it's brief and easy. Unfortunately, however, you need to know the Java way to understand the Jython way. For example, the only way to know that JButton supports an event ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required