CookBSF Tutorial

Add <script> tag

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

CookBSF.setupTagLibrary (CookSwing.getSwingTagLibrary ());

Tag usage

The type of the object this tag creates depends on the script. Script can be inserted in two ways: inside the tag, or in another file/resource specified using src attribute.

<button text="Button 1">
	<script language="beanshell" func="addActionListener">
		import java.awt.event.ActionListener;

actionPerformed( e ) { bsf.lookupBean ("statusBar").setText (e.getSource ().getText () + " Pressed"); }

return (ActionListener)this; </script> </button> <button text="Button 2"> <!-- calling an external script code (identical to the script above) --> <script func="addActionListener" src="examples/cookbsh/action.bsh"/> </button>

The language of the script can be determined in two ways, either by explicitly setting the language attribute, or by the file extension of the external script code.

return attribute

For languages that cannot return a variable at end of the script (e.g. Jython), the return attribute is required to indicate which variable to read back at end of script execution. It also affects the function CookBSF is going to call. If return attribute is specified, BSFManager.exec () function is called, otherwise BSFManager.eval () function is called. For languages such as Jython, it makes a big difference. In this case, if you intend to run a procedure that does not return a value, use return="". For example:

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

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

bsf.registerBean ("action", ExitAction ()); </script> </button> <button text="Button 2"> <!-- calling an external script code (identical to the script above) --> <script src="examples/cookbsf/action.py" return="action" func="addActionListener"/> </button>