CookJython Tutorial

To add <jython> to your existing tag library, say CookSwing, add the following code:

CookJython.setupTagLibrary (CookSwing.getSwingTagLibrary ());

There are three attributes associated with the tag.

AttributeDescription
returnSince Python does not have a way of returning a value outside functions, what you can do is to assign the return value to a variable in Python, then specify the variable in XML using this attribute.
classIn most cases, the return value's Java class type can be automatically generated. If not, then you can explicitly specify the Java class object to be generated from the Python code.
srcJython code can be inserted in two ways: inside the tag, or in another resource/file specified using "src" attribute.

Note that Python interpreter is sensitive to indentations, so make sure you do it correctly in XML.

<button text="Button 1">
	<jython return="action" func="addActionListener">
from java.awt.event import ActionListener

class ExitAction (ActionListener): def actionPerformed(self, e): statusBar.setText (e.getSource ().getText () + " Pressed");

action = ExitAction () </jython> </button> <button text="Button 2"> <!-- calling an external Jython code (identical to the Jython code above) --> <jython func="addActionListener" src="examples/cookjython/action.py" return="action"/> </button>