Benjamin Reitzammer wrote:
Hi,
On 2/14/06, neil.benn <[EMAIL PROTECTED]> wrote:>     This returns 
'NeilNeilNeilNeilNeil' - all good so far.  However I want to> be able to pass Java variables 
to Jython (I'll include JRuby and Groovy again> at some point).  For this, I reckon I need to 
use the apply function.> However, I'm confused - the docs state that I need :
I don't have the javadoc at hand (it would be great if the javadoccould be 
found on the jakarta BSF website, BTW), but looking at mycode, I can tell that 
you need thedeclareBean()-method from the BSFManager class. Something like this:
BSFManager manager = new BSFManager();manager.declareBean("objList", new 
ArrayList(), List.class);
This code makes a variable called "objList" available to your script.
A non-official but fairly recent online version of the BSF Javadocs can be found at: <http://wi.wu-wien.ac.at/rgf/rexx/bsf4rexx/current/docs/docs.apache.bsf/>. Infos on apply() can be found in: <http://wi.wu-wien.ac.at/rgf/rexx/bsf4rexx/current/docs/docs.apache.bsf/org/apache/bsf/engines/java/package-summary.html>.

---

In case you have not already found the BSF supplied P|Jython example ("calculator.py"), here it is, showing how you can interact with Java via BSF:

-------------------- cut here ------------------
"""\
A silly little calculator implemented in JPython using
Java components for the UI.
"""

import java
from java import awt

def exit(e): java.lang.System.exit(0)

def getField (f):
        t = f.getText ()
        if t == '':
                return 0
        else:
                return java.lang.Integer.parseInt (t)

def doMath (e):
        n1 = getField (f1)
        n2 = getField (f2)
        sum.setText (repr (n1 + n2))
        diff.setText (repr (n1 - n2))
        prod.setText (repr (n1 * n2))
        quo.setText (repr (n1 / n2))

f = awt.Frame ('BSH Calculator (jpython)', windowClosing=exit)

f1 = awt.TextField (20, actionPerformed=doMath)
f2 = awt.TextField (20, textValueChanged=doMath)

p = awt.Panel ()
p.setLayout (awt.GridLayout (2, 2))
p.add (awt.Label ('Enter Operand'))
p.add (f1)
p.add (awt.Label ('Enter Operand'))
p.add (f2)

f.add ('North', p)

f.add ("Center", awt.Label ('Results:'))

p = awt.Panel ()
p.setLayout (awt.GridLayout (4, 2))
p.add (awt.Label ('Sum'))
sum = awt.TextField (20)
p.add (sum)
p.add (awt.Label ('Difference'))
diff = awt.TextField (20)
p.add (diff)
p.add (awt.Label ('Product'))
prod = awt.TextField (20)
p.add (prod)
p.add (awt.Label ('Quotient'))
quo = awt.TextField (20)
p.add (quo)
f.add ('South', p)

f.pack ()
f.show ()
f.toFront ()

-------------------- cut here ------------------

You would invoke that script like:

   java org.apache.bsf.Main -in calculator.py -mode exec

HTH,

---rony

Reply via email to