Skip to content

Instantly share code, notes, and snippets.

@aterai
Created September 16, 2013 11:25
Show Gist options
  • Save aterai/6579457 to your computer and use it in GitHub Desktop.
Save aterai/6579457 to your computer and use it in GitHub Desktop.
Swing JButton ActionListener(Anonymous Inner Class) implicit closure conversion
include Java
java_import java.awt.Dimension
java_import javax.swing.JPanel
java_import javax.swing.JButton
java_import javax.swing.JTextField
class MainPanel < JPanel
def initialize
super
field = JTextField.new 32
button = JButton.new "add a"
button.add_action_listener { |e|
puts e
field.text = field.text + "a"
}
self.add field
self.add button
self.preferred_size = Dimension.new(320, 240)
end
end
java_import javax.swing.UIManager
java_import javax.swing.WindowConstants
def create_and_show_GUI
begin
UIManager.look_and_feel = UIManager.system_look_and_feel_class_name
rescue Exception => e
proxied_e = JavaUtilities.wrap e.cause
proxied_e.print_stack_trace
end
frame = javax.swing.JFrame.new "JRuby Swing JButton ActionListener"
frame.default_close_operation = WindowConstants::EXIT_ON_CLOSE
frame.content_pane.add MainPanel.new
frame.pack
frame.location_relative_to = nil
frame.visible = true
end
def run
create_and_show_GUI
end
java.awt.EventQueue.invokeLater self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment