Skip to content

@rdp /easy_way.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
example code of how to do an "on_click" method for a JButton
require 'java'
include_class 'javax.swing.JButton'
include_class 'javax.swing.JFrame'
include_class 'javax.swing.JLabel'
include_class 'java.awt.event.ActionListener'
class ClickAction
include ActionListener
def initialize &block
@block = block
raise unless block_given?
end
def actionPerformed(event)
@block.call
end
end
class JButton
def on_clicked &block
# maybe it can only have one on click handler for now?
handler = ClickAction.new &block
add_action_listener handler
end
end
class MainWindow < JFrame
def initialize
super "JRuby/Swing Demo"
button = JButton.new( "Click me!")
button.on_clicked {
p 'clicked good'
}
add button
pack
end
end
MainWindow.new.show
require 'java'
include_class 'javax.swing.JButton'
include_class 'javax.swing.JFrame'
include_class 'javax.swing.JLabel'
include_class 'java.awt.event.ActionListener'
class JButton
def on_clicked &block
# maybe it can only have one on click handler for now?
add_action_listener do |e|
block.call
end
end
end
class MainWindow < JFrame
def initialize
super "JRuby/Swing Demo"
button = JButton.new( "Click me!")
button.on_clicked {
p 'clicked good'
}
add button
pack
end
end
MainWindow.new.show
@rdp
Owner

"even easier way" gleaned from http://www.ruby-forum.com/topic/479162#962470

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.