|
This extension basically adds a <jython> tag, which supports Jython in CookXml. Each time this tag is used, a new instance of PythonInterpreter is created. Objects created using XML can be referenced using their id. Personally I am not a big fan of scripting inside XML, since excessive use of scripts instead of actual Java code is meaningless in terms of developemental efforts and performance. However, from time to time I do find myself wishing to have extra flexibilities in designing certain aspects of GUI using XML. See the demo offered below to see what I mean. TutorialTo add <jython> to your existing tag library, say CookSwing, add the following code: CookJythonLib.setupTags (CookSwing.getSwingTagLibrary ()); There are three attributes associated with the tag.
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> DemoTry the Java Web Start demo for some simple demos. Note that Jython has trouble loading Java classes that are not in the same jar file as Jython in Java Web Start. Therefore, if you intend to write a Java Web Start applicationi using Jython, I would suggest you to repackage Jython, CookXml and other Java classes that you will ever need in Python codes into a single big jar file named jython.jar. |