Accesing Java superclass atribute from JRuby subclass


#1

Hi,
I’m using JRuby 1.1.6RC1 and I would like to do something like this:

####Java Class####

public Class JavaFrame extends javax.swing.JFrame{

private javax.swing.JButton okButton;

....

}

####JRuby Class####

class RubyFrame < JavaFrame

 def initialize do

     @okButton.addActionListener do
         #do something
     end

 end

end

I’ve done this and it doesn’t work. How can I have access to this
‘okButton’
in the JRuby subclass?

My intention here is to use a Java GUI Builder (like Netbeans) to build
the
user interface and JRuby to deal with the logic.
I’ve done some research and found that MonkeyBars project (
http://monkeybars.rubyforge.org) almost do waht I want, and it uses
reflection
to give access to the property.

Luiz


#2

I think the problem is that the field you are trying to access is
private (which is not visible to subclasses in the Java world). In
this particular case the only option you have is to use reflection.

./alex

.w( the_mindstorm )p.
Alexandru P.

On Wed, Dec 31, 2008 at 1:07 PM, Luiz Fernando Signorelli Gonçalves


#3

You should be able to add:

attr_reader :okButton

and then be able to call it:

okButton

Note the lack of ‘@’ sigil.

This should work with anything JRuby 1.1.5 or later.

-Tom

On Wed, Dec 31, 2008 at 5:07 AM, Luiz Fernando Signorelli Gonçalves
removed_email_address@domain.invalid wrote:

I’ve done this and it doesn’t work. How can I have access to this ‘okButton’


Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#4

I just tried defining getters and setters like Trejkaz said and it
works.

Now my code looks like this:

####Java Class####

public Class JavaFrame extends javax.swing.JFrame{

private javax.swing.JButton okButton;

public JButton getOkButton() {
    return okButton;
}

public void setOkButton(JButton okButton) {
    this.okButton = okButton;
}
....

}

####JRuby Class####

class RubyFrame < JavaFrame

 def initialize do

     super
     ok_button.addActionListener do
        puts "OK"
     end

 end

end

I tried the attr_reader too, but i did not work:

####JRuby Class####

class RubyFrame < JavaFrame

 attr_reader :okButton

 def initialize
    super
    okButton.addActionListener do
        puts "OK"
    end
 end

end

In this case, when I try to instantiate the class I get:
initialize': undefined methodaddActionListener’ for nil:NilClass
(NoMethodError)

For now the first solution is ok to me, but I really prefer the
attr_reader aproach.
Am I doing something wrong, Thomas?

Thanks for the answers.

Luiz

On Wed, Dec 31, 2008 at 12:57 PM, Thomas E Enebo removed_email_address@domain.invalid
wrote:

private javax.swing.JButton okButton;
     end

(http://monkeybars.rubyforge.org) almost do waht I want, and it uses


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#5

Hi,

Reopening the the class did not work too. This time I tried with
simpler classes:

Java

public class JavaClass {
public String javaAtribute = “Java Atribute”;

}

JRuby

import ‘JavaClass’

class JavaClass
attr_accessor :javaAtribute
end

class RubyClass < JavaClass

end

Then, using jirb I did:

require “ruby_class”
require “ruby_class”
=> true

r = RubyClass.new
r = RubyClass.new
=> #<RubyClass:0x24ea85
@java_object=org.jruby.proxy.accessdb.JavaClass$Proxy0@1a0d346>

r.javaAtribute
r.javaAtribute
=> nil

Luiz

On Thu, Jan 1, 2009 at 5:24 PM, Thomas E Enebo removed_email_address@domain.invalid
wrote:

From what I remember (I will look tomorrow), it should look down

I just tried defining getters and setters like Trejkaz said and it works.
return okButton;

Thanks for the answers.

attr_reader :okButton

Luiz
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#6

On Jan 1, 2009, at 12:24 PM, Thomas E Enebo wrote:

Hmmm, that should work. You are setting okButton to contain something
other than null right? Assuming you are can you try this instead:

import {package to JavaFrame}.JavaFrame

Reopen JavaFrame class

class JavaFrame
attr_reader :okButton

Tom,
Do you mean field_reader/writer/accessor? (Not attr)
Just tried it in jirb (:

-Tom

public Class JavaFrame extends javax.swing.JFrame{

ok_button.addActionListener do

      puts "OK"

this:
def initialize do
in the JRuby subclass?

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Logan B.
removed_email_address@domain.invalid
602 714 1148


#7

AAARRRGGGGH :slight_smile:

I am embarrased now: field_accessor, field_reader, field_writer.

Sorry to put you down the wrong path…

-Tom

On Fri, Jan 2, 2009 at 12:29 PM, Logan B.
removed_email_address@domain.invalid wrote:

The other interesting methods to note:

}

I tried the attr_reader too, but i did not work:
super

attr_reader aproach.
On Wed, Dec 31, 2008 at 12:57 PM, Thomas E Enebo removed_email_address@domain.invalid
Note the lack of ‘@’ sigil.

}

to give access to the property.

Logan B.
removed_email_address@domain.invalid
602 714 1148


Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#8

Thomas E Enebo wrote:

AAARRRGGGGH :slight_smile:

I am embarrased now: field_accessor, field_reader, field_writer.

Sorry to put you down the wrong path…

FWIW, we don’t directly map Java fields to Ruby instance variables
because the latter are all dynamically allocated, and so we’ve always
thought it would be very confusing to have some ivars be fields on
superclasses and some ivars be normal hash-based values. The field_
methods Tom mentions above are the preferred way, since we can map them
directly to the correct Java field and where the data comes from/goes to
is masked by the wrapper methods.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#9

Hmmm, that should work. You are setting okButton to contain something
other than null right? Assuming you are can you try this instead:

import {package to JavaFrame}.JavaFrame

Reopen JavaFrame class

class JavaFrame
attr_reader :okButton
end

From what I remember (I will look tomorrow), it should look down
inheritance chain for the attribute. The above should definitely
work though.

The other interesting methods to note:
attr_writer, attr_accessor

-Tom

On Thu, Jan 1, 2009 at 12:10 PM, Luiz Fernando Signorelli Gonçalves
removed_email_address@domain.invalid wrote:

public JButton getOkButton() {
####JRuby Class####
end
class RubyFrame < JavaFrame
end

-Tom

}
end

Blog: http://www.bloglines.com/blog/ThomasEEnebo
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#10

It’s working fine now.

Thanks

On Fri, Jan 2, 2009 at 8:33 PM, Charles Oliver N.
removed_email_address@domain.invalid wrote:

would be very confusing to have some ivars be fields on superclasses and
http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email