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

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

Jeff Emanuel <>
Sent by:

2005/10/10 05:07 PM

Jack Nutting <>
Re: [Jython-users] problem subclassing/extending JLabel


class ClickableLabel(JLabel):


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:
> 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
> //

This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more.
Jython-users mailing list

This email and all content are subject to the following disclaimer: