<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: JRuby Event Proxy</title>
	<atom:link href="http://www.codecommit.com/blog/ruby/jruby-event-proxy/feed" rel="self" type="application/rss+xml" />
	<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy</link>
	<description>(permanently in beta)</description>
	<lastBuildDate>Mon, 09 Jan 2012 20:21:24 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Martin Harrigan</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-5177</link>
		<dc:creator>Martin Harrigan</dc:creator>
		<pubDate>Mon, 22 Nov 2010 14:59:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-5177</guid>
		<description>http://www.ruby-forum.com/topic/479162#962470</description>
		<content:encoded><![CDATA[<p><a href="http://www.ruby-forum.com/topic/479162#962470" rel="nofollow">http://www.ruby-forum.com/topic/479162#962470</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: roger</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-5175</link>
		<dc:creator>roger</dc:creator>
		<pubDate>Thu, 18 Nov 2010 22:46:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-5175</guid>
		<description>Or you could create a new method in the JButton class,

    button.on_clicked {
       p &#039;clicked good&#039;
     }

if you set it within the JButton class
see: 
https://gist.github.com/705822</description>
		<content:encoded><![CDATA[<p>Or you could create a new method in the JButton class,</p>
<p>    button.on_clicked {<br />
       p &#8216;clicked good&#8217;<br />
     }</p>
<p>if you set it within the JButton class<br />
see:<br />
<a href="https://gist.github.com/705822" rel="nofollow">https://gist.github.com/705822</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-2652</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Mon, 07 Jan 2008 19:38:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-2652</guid>
		<description>Hmm, I&#039;m not sure what you mean by a case where it fails.  Are you talking about creating an instance of ActionListener elsewhere (independently of the component instantiation) and then carrying it around until needed?

Replacing @block.call with &quot;instance_eval @block&quot; should solve the context problem you outlined.</description>
		<content:encoded><![CDATA[<p>Hmm, I&#8217;m not sure what you mean by a case where it fails.  Are you talking about creating an instance of ActionListener elsewhere (independently of the component instantiation) and then carrying it around until needed?</p>
<p>Replacing @block.call with &#8220;instance_eval @block&#8221; should solve the context problem you outlined.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marcin Olak</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-2651</link>
		<dc:creator>Marcin Olak</dc:creator>
		<pubDate>Mon, 07 Jan 2008 19:32:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-2651</guid>
		<description>@daniel

Block remembers context in which it was defined. So when you finally invoke:

block.call

in your example you&#039;ll most probably notice that &#039;self&#039; isn&#039;t necesserily the instance of ActionListener itself. Your example works fine however, it will cease to do so whenever you create instance of ActionListener inside some other class&#039; method. I&#039;m trying to figure out how to make it work in such cases too. Any ideas?</description>
		<content:encoded><![CDATA[<p>@daniel</p>
<p>Block remembers context in which it was defined. So when you finally invoke:</p>
<p>block.call</p>
<p>in your example you&#8217;ll most probably notice that &#8217;self&#8217; isn&#8217;t necesserily the instance of ActionListener itself. Your example works fine however, it will cease to do so whenever you create instance of ActionListener inside some other class&#8217; method. I&#8217;m trying to figure out how to make it work in such cases too. Any ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-1572</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Tue, 03 Jul 2007 17:58:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-1572</guid>
		<description>@virux

You&#039;ll have to do something with a block, like in the event proxy shown in the post.  The problem here is that the anonymous class you created doesn&#039;t have access to variables which exist solely within the scope of the Kernel override (which is what a non-class script is).  Your other options is to encase the entire example in a class and make the anonymous class a top-level class in the same way as the example class.  Not a terribly good solution IMHO.  If I were you, I&#039;d use the block proxy.</description>
		<content:encoded><![CDATA[<p>@virux</p>
<p>You&#8217;ll have to do something with a block, like in the event proxy shown in the post.  The problem here is that the anonymous class you created doesn&#8217;t have access to variables which exist solely within the scope of the Kernel override (which is what a non-class script is).  Your other options is to encase the entire example in a class and make the anonymous class a top-level class in the same way as the example class.  Not a terribly good solution IMHO.  If I were you, I&#8217;d use the block proxy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: virux</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-1577</link>
		<dc:creator>virux</dc:creator>
		<pubDate>Tue, 03 Jul 2007 17:53:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-1577</guid>
		<description>Hi i have a question.
For example i had a JTextField and a Button inside my JFrame.
I want to print the text inside the JTextField when the button is pressed.
But my problem is that i cant access the JTextField instance inside the actionPerformed method.
Ive tried this based on test3 and test2.

I then use @@textfield and it worked but i dont think it is a good way of doing this.
How can i do the same thing but without using @@

@@tfTexto = JTextField.new 15

listener = Class.new(ActionListener).new
def listener.actionPerformed(e)
  puts @@tfTexto.text
end

frame = JFrame.new &#039;Test Frame&#039;
frame.layout = FlowLayout.new
button = JButton.new &#039;Click Me&#039;
button.add @@tfTexto
button.addActionListener listener

frame.content_pane.add button

frame.setSize(200, 75)
frame.default_close_operation = JFrame::EXIT_ON_CLOSE
frame.visible = true</description>
		<content:encoded><![CDATA[<p>Hi i have a question.<br />
For example i had a JTextField and a Button inside my JFrame.<br />
I want to print the text inside the JTextField when the button is pressed.<br />
But my problem is that i cant access the JTextField instance inside the actionPerformed method.<br />
Ive tried this based on test3 and test2.</p>
<p>I then use @@textfield and it worked but i dont think it is a good way of doing this.<br />
How can i do the same thing but without using @@</p>
<p>@@tfTexto = JTextField.new 15</p>
<p>listener = Class.new(ActionListener).new<br />
def listener.actionPerformed(e)<br />
  puts @@tfTexto.text<br />
end</p>
<p>frame = JFrame.new &#8216;Test Frame&#8217;<br />
frame.layout = FlowLayout.new<br />
button = JButton.new &#8216;Click Me&#8217;<br />
button.add @@tfTexto<br />
button.addActionListener listener</p>
<p>frame.content_pane.add button</p>
<p>frame.setSize(200, 75)<br />
frame.default_close_operation = JFrame::EXIT_ON_CLOSE<br />
frame.visible = true</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshua Gertzen</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-1563</link>
		<dc:creator>Joshua Gertzen</dc:creator>
		<pubDate>Thu, 17 May 2007 07:36:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-1563</guid>
		<description>I&#039;m glad I found this post because I&#039;m trying to integrate JRuby with the ThinWire framework, which has been working well, but after getting all the bindings panned out I realized that event listener binding was pretty cumbersome.  The samples provided take the very verbose approach that you first outline, which is a smack in the face coming from Java and anonymous classes.

In any case, I&#039;ve worked extensively with the Mozilla Rhino JavaScript engine in the past and it provides a very clean binding layer for listeners/anonymous classes that I thought I&#039;d mention here:

IN JavaScript:

comp.addActionListener(function(event) {
   //do what you need to do for the actionPerformed method
});

I would imagine that with JRuby, an equivalent would be:

comp.add_action_listener {&#124;event&#124;
   //do what you need to do for the actionPerformed method
}

It should be possible for the JRuby engine to determine that the required type for addActionListener is of type ActionListener and that there is only one method on the interface definition, therefore the block should be bound to that.

I hope JRuby integrates something like that in the future.  I&#039;ll use the anonymous syntax described by Martin in the meantime.</description>
		<content:encoded><![CDATA[<p>I&#8217;m glad I found this post because I&#8217;m trying to integrate JRuby with the ThinWire framework, which has been working well, but after getting all the bindings panned out I realized that event listener binding was pretty cumbersome.  The samples provided take the very verbose approach that you first outline, which is a smack in the face coming from Java and anonymous classes.</p>
<p>In any case, I&#8217;ve worked extensively with the Mozilla Rhino JavaScript engine in the past and it provides a very clean binding layer for listeners/anonymous classes that I thought I&#8217;d mention here:</p>
<p>IN JavaScript:</p>
<p>comp.addActionListener(function(event) {<br />
   //do what you need to do for the actionPerformed method<br />
});</p>
<p>I would imagine that with JRuby, an equivalent would be:</p>
<p>comp.add_action_listener {|event|<br />
   //do what you need to do for the actionPerformed method<br />
}</p>
<p>It should be possible for the JRuby engine to determine that the required type for addActionListener is of type ActionListener and that there is only one method on the interface definition, therefore the block should be bound to that.</p>
<p>I hope JRuby integrates something like that in the future.  I&#8217;ll use the anonymous syntax described by Martin in the meantime.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Plöger</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-1211</link>
		<dc:creator>Martin Plöger</dc:creator>
		<pubDate>Mon, 12 Mar 2007 21:21:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-1211</guid>
		<description>@daniel

Thanks for your reply. I was just searching for an easy way of defining anonymous classes.
And, maybe I can&#039;t hide my java-roots but this syntax seemed easy to understand and easy to define without any auxiliary method or library. (Okay, a little change like that what you posted is still necessary). I believe that I would use the proxy_method in some cases and the other in some other cases. Both seems acceptable to me.</description>
		<content:encoded><![CDATA[<p>@daniel</p>
<p>Thanks for your reply. I was just searching for an easy way of defining anonymous classes.<br />
And, maybe I can&#8217;t hide my java-roots but this syntax seemed easy to understand and easy to define without any auxiliary method or library. (Okay, a little change like that what you posted is still necessary). I believe that I would use the proxy_method in some cases and the other in some other cases. Both seems acceptable to me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-1210</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Sat, 10 Mar 2007 19:32:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-1210</guid>
		<description>@martin

Honestly, I wasn&#039;t aware of that particular JRuby syntax trick.  That does have certain advantages, but it&#039;s still a little bulky syntactically.  It would depend on my mood which I would prefer (proxy_method or JavaProxy).  :-)

Incidentally, you can make this work like so:

module JavaClassProxy
def initialize(&amp;block)
block.call
end
end

ActionListener = java.awt.event.ActionListener

class ActionListener
include JavaClassProxy
end

ActionListener.new do
def actionPerformed(e)  # we do need the method parameter
puts &quot;clicked&quot;
end
end

I haven&#039;t tested the above code, but afaik it should work just fine.</description>
		<content:encoded><![CDATA[<p>@martin</p>
<p>Honestly, I wasn&#8217;t aware of that particular JRuby syntax trick.  That does have certain advantages, but it&#8217;s still a little bulky syntactically.  It would depend on my mood which I would prefer (proxy_method or JavaProxy).  <img src='http://www.codecommit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Incidentally, you can make this work like so:</p>
<p>module JavaClassProxy<br />
def initialize(&amp;block)<br />
block.call<br />
end<br />
end</p>
<p>ActionListener = java.awt.event.ActionListener</p>
<p>class ActionListener<br />
include JavaClassProxy<br />
end</p>
<p>ActionListener.new do<br />
def actionPerformed(e)  # we do need the method parameter<br />
puts &#8220;clicked&#8221;<br />
end<br />
end</p>
<p>I haven&#8217;t tested the above code, but afaik it should work just fine.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Plöger</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-event-proxy/comment-page-1#comment-1208</link>
		<dc:creator>Martin Plöger</dc:creator>
		<pubDate>Sat, 10 Mar 2007 15:55:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.danielspiewak.com/java/jruby-event-proxy#comment-1208</guid>
		<description>I think the way of proxying the Java Listener classes is a great way of keeping the code clean and ruby-like. But isn&#039;t there possibly an even cleaner way of creating anonymous classes out of JRuby.

module Swing
  include_package &#039;javax.swing&#039;
  include_package &#039;java.awt&#039;
  include_package &#039;java.awt.event&#039;
end

button.add_action_listener(Swing::ActionListener.new do
  def actionPerformed event
    puts &quot;Click!&quot;
  end
end)

Is it possible to provide the Java-Proxy-Classes with a block, which is then evaluated to add the defined methods to the proxy?
This would be similar to the concept of anonymous classes in Java. The lower code makes it possible to define methods via block evaluated by the initialize-method, but these are private, but it shows the concept. Methods defined via Module::define_method are public. Maybe that can be used to solve this problem.

I think a way that does not need thousands of Listener classes like the way with the proxy_method is good, but a way without calling a helper-method is even better. A way of defining anonymous classes
in a way that uses blocks seems best, because it is easy an can even be understood by Java-guys.

Is this possible or am I on the wrong way?

PSEUDO_CODE (does not work, but shows what I mean):
------------------------------------------

class JavaProxy
  def initialize &amp;block
    block.call # evaluates the block
  end
end

include_class &#039;java.awt.event.ActionListener&#039; #ActionListener is one of the JavaProxies

ActionListener.new do
  def actionPerformed # define the method in a block, which is then evaluated
    puts &quot;CLICK!&quot;
  end
end</description>
		<content:encoded><![CDATA[<p>I think the way of proxying the Java Listener classes is a great way of keeping the code clean and ruby-like. But isn&#8217;t there possibly an even cleaner way of creating anonymous classes out of JRuby.</p>
<p>module Swing<br />
  include_package &#8216;javax.swing&#8217;<br />
  include_package &#8216;java.awt&#8217;<br />
  include_package &#8216;java.awt.event&#8217;<br />
end</p>
<p>button.add_action_listener(Swing::ActionListener.new do<br />
  def actionPerformed event<br />
    puts &#8220;Click!&#8221;<br />
  end<br />
end)</p>
<p>Is it possible to provide the Java-Proxy-Classes with a block, which is then evaluated to add the defined methods to the proxy?<br />
This would be similar to the concept of anonymous classes in Java. The lower code makes it possible to define methods via block evaluated by the initialize-method, but these are private, but it shows the concept. Methods defined via Module::define_method are public. Maybe that can be used to solve this problem.</p>
<p>I think a way that does not need thousands of Listener classes like the way with the proxy_method is good, but a way without calling a helper-method is even better. A way of defining anonymous classes<br />
in a way that uses blocks seems best, because it is easy an can even be understood by Java-guys.</p>
<p>Is this possible or am I on the wrong way?</p>
<p>PSEUDO_CODE (does not work, but shows what I mean):<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>class JavaProxy<br />
  def initialize &amp;block<br />
    block.call # evaluates the block<br />
  end<br />
end</p>
<p>include_class &#8216;java.awt.event.ActionListener&#8217; #ActionListener is one of the JavaProxies</p>
<p>ActionListener.new do<br />
  def actionPerformed # define the method in a block, which is then evaluated<br />
    puts &#8220;CLICK!&#8221;<br />
  end<br />
end</p>
]]></content:encoded>
	</item>
</channel>
</rss>

