Forum: JRuby How to create a button class that uses blocks instead of listeners?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-11-26 17:45
(Received via mailing list)
I'd like a JRuby button class that's a subclass of the Swing button
class, but that allows one to say something like:

Rbutton.new("Click me") { puts "Clicked!" }

What I have so far is

require 'java'

class Rbutton < javax.swing.JButton
   def initialize(text)
     super(text)
     if block_given?
       listener = java.awt.event.ActionListener.new
       def listener.actionPerformed(event)
         yield
       end
       self.addActionListener(listener)
     end
   end
end

frame = javax.swing.JFrame.new("Window")
label = Rbutton.new("Hello") {puts "Hello"}

frame.getContentPane.add(label)
frame.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE)
frame.pack
frame.setVisible(true)

The problem, of course, is that the yield is out of scope when it's
called. Anyone know of a solution to this?

Thanks,
Ken McDonald

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
13313ac2ec7ba7c43b1b952db034ff3b?d=identicon&s=25 Thomas E Enebo (Guest)
on 2008-11-26 17:56
(Received via mailing list)
require 'java'

class Rbutton < javax.swing.JButton
 def initialize(text)
   super(text)
   self.addActionListener Proc.new if block_given?
 end
end

frame = javax.swing.JFrame.new("Window")
label = Rbutton.new("Hello") {puts "Hello"}

frame.getContentPane.add(label)
frame.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE)
frame.pack
frame.setVisible(true)


On Wed, Nov 26, 2008 at 10:45 AM, Kenneth McDonald
<kenneth.m.mcdonald@sbcglobal.net> wrote:
>  def initialize(text)
>
>
> Thanks,
> Ken McDonald
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>
>



--
Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: enebo@acm.org , tom.enebo@gmail.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-11-26 18:08
(Received via mailing list)
Sweet! Thank you!

Ken


On Nov 26, 2008, at 10:55 AM, Thomas E Enebo wrote:

> label = Rbutton.new("Hello") {puts "Hello"}
>> class,
>>   super(text)
>> frame = javax.swing.JFrame.new("Window")
>>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
1669f7561643664344af30ef1e29b611?d=identicon&s=25 Werner Schuster (murphee) (Guest)
on 2008-11-26 18:09
(Received via mailing list)
Kenneth McDonald wrote:
> The problem, of course, is that the yield is out of scope when it's
> called. Anyone know of a solution to this?
Capture the block with a closure.
class Rbutton < javax.swing.JButton
  def initialize(text, &bl)
    super(text)
    if block_given?
      self.addActionListener() {
        bl.call
      }
    end
  end
end


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
This topic is locked and can not be replied to.