Call the __init__ method of the super class and define your Event method, do not use the same name of the method you are overriding.

from javax.swing import *

class ClickableLabel(JLabel):
    def __init__(self, *args, **kwargs):                
       JLabel.__init__(self, *args, **kwargs)                    
       self.mouseClicked = self.OnMouseClick
or replace the above two lines with the following line
          JLabel.__init__(self, mouseClicked=self.OnMouseClick, *args, **kwargs )

    def OnMouseClick(self, evt):
        print "clicking"    


frame = JFrame("Testing Clickable Label")
label = ClickableLabel(text="Click Me");
frame.contentPane.add(label, "Center");
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.show()

This way you can just implement the event methods for only those events you are interested in trapping.

Hope this helps.

Ronnie Z. Dlomo
+27 12 673 7574
rdlomo@momentum.co.za







Jeff Emanuel <jemanuel@frii.com>
Sent by: jython-users-admin@lists.sourceforge.net

2005/10/10 05:07 PM

To
Jack Nutting <jnutting@gmail.com>
cc
Jython-users@lists.sourceforge.net
Subject
Re: [Jython-users] problem subclassing/extending JLabel





Change

class ClickableLabel(JLabel):

to

class ClickableLabel(JLabel,MouseListener):

so that your class extends JLabel and implements MouseListener.



Jack Nutting wrote:
> Hi all,
>
> I'm trying to implement a subclass of JLabel that extends JLabel to notice
> mouse clicks. I found a tutorial that explains how to do this (in Java)
> here: http://www.faqs.org/docs/javap/c7/s4.html
>
> The main points seem to be declaring the class like this:
>
> public class StopWatch extends JLabel implements MouseListener
>
> followed by calling "addMouseListener(this)" in the constructor, and
> implementing a few "mouseXxxx" methods.
>
> I tried to follow this pattern in jython by doing this:
>
> class ClickableLabel(JLabel):
> def init(self):
> JLabel.init(self)
> self.addMouseListener(self)
>
> def mousePressed(self, evt):
> print "clicked"
>
> def mouseReleased(self, evt):
> pass
> def mouseClicked(self, evt):
> pass
> def mouseEntered(self, evt):
> pass
> def mouseExited(self, evt):
> pass
>
> Unfortunately it doesn't work. That is, I can create an instance of this and
> put it in a window, and it behaves like a JLabel, but it doesn't print the
> message when I click. My best guess is that this has something to do with
> the "implements MouseListener" in the Java version, which I'm not doing
> anything to match in the jython version. Is there some particular way of
> declaring that my class implements a Java interface?
>
> --
> // jack
> // http://www.nuthole.com
>


-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users

This email and all content are subject to the following disclaimer: http://content.momentum.co.za/content/legal/disclaimer_email.htm