Jython question concerning actionPerformed

Ype Kingma ykingma at accessforall.nl
Mon Feb 4 14:34:54 EST 2002


Mishre,

you wrote:
> 
> I have the following code in Jython running on JDK 1.4, which looks
> like it should work, but isn't:
> 
> def actionPerformed(self,event):
>     if event.getActionCommand() == "Exit":
>         java.lang.System.exit(0)
>     elif event.getActionCommand() == "comboBoxChanged":
>         print event
> 
> Then later on I have:
> 
> self.catCombo = javax.swing.JComboBox(actionPerformed=self.actionPerformed)
>

Have a look at http://www.jython.org/docs/properties.html:
you can pass an ActionListener object, or a function with one argument.

You might try one of these: (untested)

self.catCombo = javax.swing.JComboBox(self)
# This assumes that actionPerformed method is available
# and that the current class implements ActionListener.

or

self.catCombo = javax.swing.JComboBox(actionPerformed = (lamda event, self=self:
                                                          self.actionPerformed(event))
# No need to inherit from ActionListener here.

I prefer to use the last form since it allows to choose a method from the current
class to be used as action listener.
You might call eg. self.exit(event) from the lambda.
 
> 
> Does anyone have any ideas on what to try next?
> 
> Thanks,
> 

My pleasure,
Ype



More information about the Python-list mailing list