Inheriting from javax.swing.JComponent

Hi all,

I have a little sample application I’m trying to convert from Jython.
This
is the original jython code:

from javax.swing import *
from java.awt import *
from java.lang import *

class TestPanel(JComponent):
def init(self):
self.setBackground(Color.black)
self.text = “Testing”
def paintComponent(self, g):
r = self.getBounds()
g.setColor(Color.black)
g.fillRect(0, 0, r.width, r.height)
g.setColor(Color.white)
g.drawString(self.text, 50, 50)

if name == “main”:
panel = TestPanel()
frame = JFrame(“Jython Test Panel”, size=(260,200))
frame.contentPane.add(panel)
frame.windowClosing = lambda e: System.exit(0)
frame.show()
panel.repaint()

My naive translation to JRuby looks like this:

require “java”

include_class “javax.swing.JComponent”
include_class “javax.swing.JFrame”
include_class “java.awt.Color”
include_class “java.lang.System”

class TestPanel < JComponent

def initialize()
self.setBackground(Color.black)
self.text = “Testing”
end

def paintComponent(g)
r = self.getBounds()
g.setColor(Color.black)
g.fillRect(0, 0, r.width, r.height)
g.setColor(Color.white)
g.drawString(self.text, 50, 50)
end

end

panel = TestPanel.new()
frame = JFrame.new(“JRuby Test Panel”)
frame.contentPane.add(panel)
frame.show()
panel.repaint()

When I get to this line:

panel = TestPanel.new()

I get the following error message:

file:/F:/jruby/lib/jruby.jar!/builtin/javasupport/proxy/concrete.rb:6:in
new':invokee not a java object (TypeError) from file:/F:/jruby/lib/jruby.jar!/builtin/javasupport/proxy/concrete.rb:6:in new’
from TestPanel.rb:25

I assume this occurs because JComponent is abastract. How do I modify
my
code to make this work correctly?

Thanks,
John

View this message in context:
http://www.nabble.com/Inheriting-from-javax.swing.JComponent-tf4635599.html#a13238438
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Try adding a super() into the initialize method of the TestPanel class.

Cheers!

That fixed it. Thanks.

Rui wrote:

I have a little sample application I’m trying to convert from
self.text = “Testing”
frame.contentPane.add(panel)
include_class “java.awt.Color”
r = self.getBounds()
frame.contentPane.add(panel)
`new’:invokee not a java object (TypeError)


rui
Um cara cariobárico


View this message in context:
http://www.nabble.com/Inheriting-from-javax.swing.JComponent-tf4635599.html#a13240967
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email