|
From: Bob G. <bg...@us...> - 2012-09-27 11:19:32
|
I ran into something today that frustrates me, and I'm hoping that someone might be able to help me understand if I am making a mistake, or if by some chance I've stumbled upon a bug. While working with Jython Swing applications, I encountered something that I couldn't explain. Some of the Java Swing Tutorial applications use the following expression to get the name of object. For example, one example can be found here: -------------------------------------------------------------------------------- http://docs.oracle.com/javase/tutorial/uiswing/examples/events/ComponentEventDemoProject/src/events/ComponentEventDemo.java -------------------------------------------------------------------------------- All of the ComponentListener methods use the following expression to obtain the name of the component for which an event was generated: e.getComponent().getClass().getName() In case you are interested, the source code for the componentHidden method is: -------------------------------------------------------------------------------- public void componentHidden(ComponentEvent e) { displayMessage(e.getComponent().getClass().getName() + " --- Hidden"); } -------------------------------------------------------------------------------- Unfortunately, when I try to use this same technique in Jython, the following error message is generated: -------------------------------------------------------------------------------- TypeError: getName(): expected 1 args; got 0 -------------------------------------------------------------------------------- The really strange thing about this is that the only getName method that I can find in the class hierarchy is the one in java.awt.Component and it doesn't expect any arguments: -------------------------------------------------------------------------------- http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getName%28%29 -------------------------------------------------------------------------------- I was able to work around this, but it is still something that bugs me because I don't understand what is happening. Can anyone explain why I'm getting this error? Here is a small script (with line numbers) to demonstrate the error: 1| #------------------------------------------------------------------------------- 2|# Command: possibleBug 3|# Purpose: Jython script to demonstrate strange behavior / possible bug with 4|# Swing applications 5|# Usage: jython possibleBug.py 6| #------------------------------------------------------------------------------- 7| 8|import java 9|import sys 10|from java.awt import EventQueue 11|from javax.swing import JButton 12|from javax.swing import JFrame 13| 14| #------------------------------------------------------------------------------- 15|# Name: possibleBug() 16|# Role: Jython Swing application class used to demonstrate the issue being seen. 17|# Note: This class should be instantiated on the Swing Event Dispatch Thread 18| #------------------------------------------------------------------------------- 19|class possibleBug( java.lang.Runnable ) : 20| 21| #--------------------------------------------------------------------------- 22| # Name: run() 23| # Role: Create a frame containing a single button 24| # Note: Invoked by the Swing Event Dispatch Thread 25| #--------------------------------------------------------------------------- 26| def run( self ) : 27| frame = JFrame( 28| 'possibleBug', 29| locationRelativeTo = None, 30| defaultCloseOperation = JFrame.EXIT_ON_CLOSE 31| ) 32| frame.add( 33| JButton( 34| 'Press', 35| actionPerformed = self.pressed 36| ) 37| ) 38| frame.pack() 39| frame.setVisible( 1 ) 40| 41| #--------------------------------------------------------------------------- 42| # Name: pressed() 43| # Role: Used as button ActionListener.actionPerformed() event handler 44| #--------------------------------------------------------------------------- 45| def pressed( self, event ) : 46|# print ' event:', event 47|# print 'Source:', event.getSource() 48|# print ' class:', event.getSource().getClass() 49| print ' name:', event.getSource().getClass().getName() 50| 51| #------------------------------------------------------------------------------- 52|# Name: anonymous 53|# Role: Verify that the script was executed, and not imported and instantiate 54|# the user application class on the Swing Event Dispatch Thread 55| #------------------------------------------------------------------------------- 56|if __name__ in [ '__main__', 'main' ] : 57| EventQueue.invokeLater( possibleBug() ) 58|else : 59| print '\nError: This script should be executed, not imported.\n' 60| print 'Usage: jython %s.py' % __name__ 61| sys.exit() When I execute the script, and press the button, the following exception can be seen: -------------------------------------------------------------------------------- name:Exception in thread "AWT-EventQueue-0" Traceback (most recent call last): File "possibleBug.py", line 49, in pressed print ' name:', event.getSource().getClass().getName() TypeError: getName(): expected 1 args; got 0 -------------------------------------------------------------------------------- I'm using Jython 2.5.3: -------------------------------------------------------------------------------- Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36) [IBM J9 VM (IBM Corporation)] on java1.6.0 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print sys.version 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36) [IBM J9 VM (IBM Corporation)] >>> print sys.version_info (2, 5, 3, 'final', 0) >>> quit() -------------------------------------------------------------------------------- Thanks Bob |