Here's a simple JRuby application that shows how to integrate Ruby with Java Swing.
include Java
include_class java.awt.event.ActionListener
include_class java.lang.Runnable
include_class javax.swing.JButton
include_class javax.swing.JFrame
include_class javax.swing.JPanel
include_class javax.swing.JProgressBar
include_class javax.swing.JOptionPane
include_class javax.swing.SwingUtilities
class JRubySwing
def initialize
button = JButton.new("Click Me!")
progress_bar = JProgressBar.new(0,100)
button.add_action_listener(ActionListener.impl {
button.set_enabled(false)
time_consuming_method(progress_bar,button)
})
panel = JPanel.new
panel.add(button)
panel.add(progress_bar)
frame = JFrame.new("Hello Swing")
frame.get_content_pane.add(panel)
frame.set_default_close_operation(JFrame::EXIT_ON_CLOSE)
frame.pack
frame.set_location_relative_to(nil)
frame.visible = true
end
def time_consuming_method(progress_bar,button)
t = java.lang.Thread.new(r = Runnable.impl {
1.upto 100 do |i|
sleep(0.01)
progress_bar.set_value(i)
end
SwingUtilities.invokeLater(Runnable.impl { button.set_enabled(true) })
})
t.start
end
end
JRubySwing.new