• Select a language



Java* and Python*, Together at Last
Page & Feed Options
Print this
Bookmark This
Digg this | Add to your del.icio.us account
Table of Contents

Graphical Programming
While printing text is easy and doesn't require much understanding of Python, working with the various Java libraries is where Jython gets interesting. To demonstrate, we'll create a simple little program with a text field, a list, and a button, so that when you click the button, the text in the text field is added to the list.

In the Java programming language, you must import a library before you can use it. With Jython, simply use the from statement. For example, to do the equivalent of import java.awt.*; for a graphical Jython program, use this command:

from java import awt

That makes the classes in the java.awt package available. Now, to access a class like the java.awt.Frame class, use awt.Frame, as in the following line that will create one for us.

frame = awt.Frame("Hello, Jython")


To reference the Frame class directly, without the awt prefix, import the class explicitly:

from java.awt import Frame

frame = Frame("Hello, Jython")



# file list.py

from java import awt

from java.awt import Frame

from java.awt import event

from java.lang import System



frame = Frame("Hello, Jython")

frame.setSize(200, 200)



class ExitHandler(event.WindowAdapter):

  def windowClosing(self, WindowEvent):

    System.exit(0)



frame.addWindowListener(ExitHandler())



text = awt.TextField()

frame.add(text, awt.BorderLayout.NORTH)



list = awt.List()

frame.add(list, awt.BorderLayout.CENTER)



button = awt.Button("Add")

button.setBackground(awt.Color.yellow)

frame.add(button, awt.BorderLayout.SOUTH)



frame.show()


JavaBeans*
JavaBeans are special classes with properties and events. They are special because they follow certain naming conventions. For instance, in the previous code, the Frame class had its size set by calling the setSize() method. The size here is a bean property of the Frame class. Essentially, the class has setSize() and getSize() methods to change and retrieve the current property value. That's really all there is to properties.

Instead of explicitly calling the property methods, Jython lets you access the property directly. So, instead of calling setSize(), use this command:

frame.size = (200, 200)


Jython first checks for a public field by the size name; if it isn't found, Jython looks for a setter method for the property. It then uses the values property to create an appropriate dimension.

Jython actually goes one step further with properties. Not only can you access them directly in this way, you can initialize them when the instance of the class is created. Move the property name into the parenthesis for the constructor call. These two lines of source code:

frame = Frame("Hello, Jython")

frame.setSize(200, 200)


can now become one:

frame = Frame("Hello, Jython", size=(200, 200)




button = awt.Button("Add",

  background=awt.Color.yellow)


Event Handling
Let's finish up the example by adding event-handling code. While you already have some to shutdown the application when the frame is closed, you'll both simplify the frame closing code and add an event handler to give the program some action.

Similar to the way Jython lets you access JavaBean properties, you can access JavaBean events. With properties, use the property name. With events, use the event handler name. So, to shutdown the frame, the handler is windowClosing. Assign the appropriate method to the frame, and you have the event handler. The term lambda tells Jython to define an anonymous function with the subsequent parameters. In this case, the parameter e is for the WindowEvent. Without lambda, the System.exit(0) call would be executed immediately.

frame.windowClosing =

  lambda e: System.exit(0)


Like initializing properties, this event handling code can be moved into the frame initialization code, to place all the initialization code for the component together:

frame = Frame("Hello, Jython", size=(200, 200),

  windowClosing = lambda e: System.exit(0))


Now create the button event handler that takes advantage of accessing bean properties and the value of the text field, and also handles events associated with selecting the button.

Button selection is the actionPerformed method of the ActionListener. So, to add an action handler, set actionPerformed for the button. To add an item to a AWT List control, pass the text string to its add() method. Finally, get the value of the text property of the AWT TextField. Here's the code that puts it all together:

button.actionPerformed = lambda e: list.add(text.text)


As with the window-closing handler, add the button's action handler to the button initialization code:

button = Button("Add",

  background=Color.yellow,

  actionPerformed = lambda e: list.add(text.text))


Prev1  2  3  4  5  Next

Page 3 of 5