From: Kevin B. <kb...@ca...> - 2002-01-18 21:34:44
|
Michael Haggerty wrote: > > Is there a way to access jython's "callable-to-listener" conversion > explicitly? You can (cheat a lot and) actually modify the JComboBox.__dict__: """ adapter.py - demo how to explicitly create an adapter for a Java interface. This adds the capability to set 'actionPerformed=callable' in a JComboBox constructor. Note that this hides the 'JComboBox.actionPerformed' method which caused the trouble in the first place, but you're not supposed to do anything with that method, so it is probably OK... *WARNING* This is a hack - it works for Jython 2.1, but probably won't work with future releases. """ from org.python.core import PyBeanEventProperty, PyBeanEvent def addEventProperty( objectClass, addMethodName, eventName, eventClass ): """Add the ability to call 'method=callable' in a constructor call. objectClass - class to modify addMethodName - objectClass method to add a listener eventName - name of event listener property ('actionListener') eventClass - class of event listener interface """ # code stolen from PyJavaClass.addEvent addMethod = objectClass.getMethod( addMethodName, [eventClass] ) # add support for assigning individual event handlers in constructor for eventMethod in eventClass.getMethods(): prop = PyBeanEventProperty( eventName, eventClass, addMethod, eventMethod ) objectClass.__dict__.__setitem__( prop.__name__, prop ) # add support for assigning 'whateverListener=WhateverListenerImpl' event = PyBeanEvent(eventName, eventClass, addMethod); objectClass.__dict__.__setitem__(event.__name__, event); # demo code from javax.swing import JComboBox, JFrame from java.awt.event import ActionListener from java.awt import Dimension, BorderLayout def showWidget( widget, name="Demo" ): f = JFrame( name ) p = f.getContentPane() p.add( widget, BorderLayout.NORTH ) f.setSize( 300, 300 ) f.show() def test(): # allow JComboBox( actionPerformed=callable ) addEventProperty( JComboBox, 'addActionListener', 'actionListener', ActionListener ) def doit( event ): print "doit:", event jc = JComboBox( range( 10 ), actionPerformed=doit ) showWidget( jc ) Cool. :-) kb |