[moving this to jython-users list]
On Sat, Mar 03, 2001 at 10:03:06PM -0800, Michael Vanier wrote:
|
| Hi,
|
| So, I've been playing with jython for a few hours, and I'm *really*
| impressed. This is a kick-ass system for doing portable graphics
| programming.
Just bear in mind that Jython runs in a JVM. Thus is brings with it
some of the limitations of the JVM. Namely Swing descends from AWT,
and AWT requires a native peer for everything. Thus you can't use,
for example, java.awt.Image or javax.swing.Timer without having a
native display. (ie no headless *nix servers unless they have an
external Xserver or Xvfb running) Large companies don't always do the
logical thing.
|
| However, the online docs are a bit sparse, and one problem I've been having
| is in trying to call a protected superclass method from a subclass which
| overrides the method. The offending superclass is javax.swing.JPanel. I
| have a subclass of this (let's call it MyJPanel) which needs to override the
| protected paintComponent() method of JPanel. The naive approach:
|
| class MyJPanel:
| # ... lots of code ...
|
| def paintComponent(self, g): # g == Graphics object
| JPanel.paintComponent(self, g)
| # extra code goes here
|
| doesn't work, because paintComponent is not accessible from the MyJPanel
| namespace.
Not quite. You are taking the class "JPanel" and trying to access
it's member "paintComponent". In Java/C++ syntax, only items declared
as "static" are actually in the /class/. All others are in
/instances/ of the class they are written in. This is one area where
Python is much more consistent.
| The online jython docs say this:
|
| In Python, if I want to call the foo method in my superclass, I use the
| form:
|
| SuperClass.foo(self)
|
in Python ... (not Java)
| Instead you have to use the "self.super_foo()" call style.
This is correct. In Python you call the method on the /class/ object
passing it the instance ("this" in Java/C++) as the first argument.
In Java, only /static/ methods can be called on the /class/.
|
| Well, I couldn't get this to work. I'm not sure if the above statement means
| that you're supposed to write (in my case) "self.super_paintComponent(g)" or
| self.JPanel_paintComponent(g) but neither one works. What is the magic
| invocation?
self.paintComponent( g )
should do the trick.
| Also, I was surprised to find that MyJPanel, just by subclassing JPanel, gets
| its own public version of the protected JPanel methods! So if I don't
| override the method, I can call it directly. I assume this is intentional,
| but maybe the jython developers can comment.
MyJPanel gets all the methods that are in JPanel (except maybe private
ones). Since Python does not have an access restriction model, they
all become public.
HTH,
-D
|