Forum: JRuby event listener with one argument

71c85a662af0764df3a77cc00db166c9?d=identicon&s=25 Raphael B. (raphael_b)
on 2013-08-29 14:07
(Received via mailing list)
Hi,

I'm developing an app with JWT (http://www.webtoolkit.eu/jwt) using
Jruby
1.6.3.
I have a problem adding a listener taking one argument:

I define a signal with one argument [1]:
     @selection_clicked = Signal1.new(self)

and trigger it with one argument:
      @selection_clicked.trigger(WImage.new(resource, "chart
coming..."))

I then inspect the instance of the signal and I'm adding a listener:
      puts @lists.selection_clicked.inspect # prints
#<Java::EuWebtoolkitJwt::Signal1:0x69127c4d>
      @lists.selection_clicked.add_listener(self) { |resource|
display_chart(resource) }

Below is the error I get, the null pointer exception being due to the
block
trying to access the argument.
The code works fine when using Listener1 and  passing a block without
argument.
When using a Signal (with no argument), I have to modify the trigger
call
to remove the argument too, so the problem occurs when attaching the
listener.

Is my JRuby code to attach the listener correct?

Thanks in advance!

Raph




:1 warning: multiple values for a block parameter (0 for 1)
16191 [qtp555989664-18] ERROR eu.webtoolkit.jwt.WebSession - error
during
event handling: org.jruby.exceptions.RaiseException: Native Exception:
'class java.lang.NullPointerException'; Message: null; StackTrace:
java.lang.NullPointerException
        at
eu.webtoolkit.jwt.WContainerWidget.addWidget(WContainerWidget.java:304)

16192 [qtp555989664-18] ERROR eu.webtoolkit.jwt.WebSession - fatal
error:
org.jruby.exceptions.RaiseException: Native Exception: 'class
java.lang.NullPointerException'; Message: null; StackTrace:
java.lang.NullPointerException
        at
eu.webtoolkit.jwt.WContainerWidget.addWidget(WContainerWidget.java:304)

org.jruby.exceptions.RaiseException: Native Exception: 'class
java.lang.NullPointerException'; Message: null; StackTrace:
java.lang.NullPointerException
        at
eu.webtoolkit.jwt.WContainerWidget.addWidget(WContainerWidget.java:304)

        at
eu.webtoolkit.jwt.WContainerWidget.addWidget(WContainerWidget.java:304)
Caused by: java.lang.NullPointerException
        ... 1 more




[1]
http://www.webtoolkit.eu/jwt/latest/doc/javadoc/eu...
71c85a662af0764df3a77cc00db166c9?d=identicon&s=25 Raphael B. (raphael_b)
on 2013-09-12 14:55
(Received via mailing list)
Looking at this again, looking for your input:
Can someone confirm that the following JRuby code is the right
translation
(regarding the addition of the event listener) of the java code below?
The
java code gets the listener's argument, the Jruby listener gets no
argument
value (e is nil...).

Ruby:
    nameEdit = WLineEdit.new
    nameEdit.keyPressed.addListener(self) do |e|
      puts '---------------------------'
      puts e.getKey().toString()
      puts '---------------------------'
    end

Java:
        nameEdit.keyPressed().addListener(this, new
Signal1.Listener<WKeyEvent>() {
            public void trigger(WKeyEvent e) {
                greeting.setText("Key pressed : " +
e.getKey().toString());
            }
        });




thanks

Raph
Caa2df9372ffa0a9e95b2bab1e8fea34?d=identicon&s=25 Karol Bucek (Guest)
on 2013-09-12 19:14
(Received via mailing list)
Hi Rapj, that (J)Ruby code of yours (with the event listener implemented
as a block) should have worked ...

Maybe it's a bug try a newer JRuby if you must use 1.6 than the latest
1.6.8 ... it really should work.

K.
71c85a662af0764df3a77cc00db166c9?d=identicon&s=25 Raphael B. (raphael_b)
on 2013-09-12 19:27
(Received via mailing list)
Hi Karol,

Thanks for your feedback!

I have tested again with this version of Jruby with the same result:
jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on OpenJDK 64-Bit Server VM
1.6.0_24-b24 [linux-amd64]

The output is:

---------------------------
17026 [qtp1272015198-18] ERROR eu.webtoolkit.jwt.WebSession - error
during
event handling: org.jruby.exceptions.RaiseException: (NoMethodError)
undefined method `getKey' for nil:NilClass
Caa2df9372ffa0a9e95b2bab1e8fea34?d=identicon&s=25 Karol Bucek (Guest)
on 2013-09-12 21:59
(Received via mailing list)
Interesting, than that's either a bug and/or smt weird with JWT.
I've tried similar code with Swing and it is working as expected :

>> window = javax.swing.JDialog.new
=> #<Java::JavaxSwing::JDialog:0x52847b05>

>> window.add_property_change_listener('visible') { |e| puts "property
visible changed: #{e}" }
=> nil

>> window.fire_property_change 'visible', true, false
property visible changed:
java.beans.PropertyChangeEvent[propertyName=visible; oldValue=true;
newValue=false; propagationId=null;
source=javax.swing.JDialog[dialog1,641,365,338x214,layout=java.awt.BorderLayout,MODELESS,title=Hello
There!,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,1,29,336x184,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]]
=> nil

K.
Caa2df9372ffa0a9e95b2bab1e8fea34?d=identicon&s=25 Karol Bucek (Guest)
on 2013-09-12 22:05
(Received via mailing list)
Actually I see it now ... there are multiple 2 argument addListener
methods on the API - it's not picking the one you're assuming.

You will need to help it to call the one you'd like ... but than of
course
the Ruby code gets a bit ugly, adjust your code accordingly :

>> listener = Proc.new { |e| puts "property visible changed: #{e}" }
=> #<Proc:0x496d5aa8@(irb):50>
>> listener = listener.to_java
java.beans.PropertyChangeListener.java_class.to_java
=> #<Java::OrgJrubyGen::InterfaceImpl1904177314:0x6cd0b6f8>
>> window.add_property_change_listener 'visible', listener
=> nil

Maybe there's a simpler way to do this with JRuby ... not sure.

K.
C37cb1a6a19e23f067aae6ccb2d0de4b?d=identicon&s=25 Manuel Ramos Caro (Guest)
on 2013-09-12 23:44
(Received via mailing list)
Hi,

Events on Swing works fine. I dont know how works on JWT but looks
similar. Maybe you aren't doing reference to the right class as Karol
said. Some heritage can do a easy readable code for you ...

Look at this Jslider example on Swing...

http://zetcode.com/gui/jrubyswing/components/

I'm developing on free time an Jruby Swing GUI and make use of a lot of
events on that way.

http://sourceforge.net/projects/rmldonkey/

Regards.
71c85a662af0764df3a77cc00db166c9?d=identicon&s=25 Raphael B. (raphael_b)
on 2013-09-14 11:58
(Received via mailing list)
Thanks Karol, the following code works indeed:

    nameEdit = WLineEdit.new
    listener = Proc.new do |k|
      puts '---------------------------'
      puts k.getKey().toString()
      puts '---------------------------'
    end
    listener = listener.to_java Signal1::Listener.java_class.to_java
    nameEdit.keyPressed.addListener(self, listener)
    page_layout.addWidget(nameEdit)

Manuel, thanks for the links, I keep them at hand!

Raph


On Thu, Sep 12, 2013 at 11:44 PM, Manuel Ramos Caro <
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.