Created
August 1, 2011 08:48
-
-
Save changmason/1117811 to your computer and use it in GitHub Desktop.
Swing progress bar demo in JRuby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Program translate from | |
# http://download.oracle.com/javase/tutorial/uiswing/examples/components/ProgressBarDemoProject/src/components/ProgressBarDemo.java | |
# 2011/08/01 first commit (by Mason) | |
require "java" | |
java_import java.awt::Toolkit | |
java_import java.awt::Cursor | |
java_import java.awt::Insets | |
java_import java.awt::BorderLayout | |
java_import javax.swing::JFrame | |
java_import javax.swing::JPanel | |
java_import javax.swing::SwingWorker | |
java_import javax.swing::JScrollPane | |
java_import javax.swing::JProgressBar | |
java_import javax.swing::BorderFactory | |
class Task < SwingWorker | |
def initialize(program, start_button, task_output) | |
super() | |
@program = program | |
@start_button = start_button | |
@task_output = task_output | |
end | |
def doInBackground | |
progress = 0 | |
set_progress(0) | |
while progress < 100 | |
progress += rand(10) | |
sleep(rand) | |
set_progress([progress, 100].min) | |
end | |
return | |
end | |
def done | |
Toolkit.get_default_toolkit.beep | |
@start_button.set_enabled(true) | |
@program.set_cursor(nil) #turn off the wait cursor | |
@task_output.append("Done!\n") | |
end | |
end | |
class ProgressBarDemo < JPanel | |
def self.create_and_show_gui | |
# Create and set up the window. | |
frame = JFrame.new("ProgressBarDemo") | |
frame.set_default_close_operation(JFrame::EXIT_ON_CLOSE) | |
# Create and set up the content pane. | |
new_content_pane = ProgressBarDemo.new | |
new_content_pane.set_opaque(true) # content panes must be opaque | |
frame.set_content_pane(new_content_pane) | |
# Display the window. | |
frame.pack | |
frame.set_visible(true) | |
end | |
def initialize | |
super(BorderLayout.new) | |
# Create the demo's UI. | |
@start_button = javax.swing::JButton.new("Start") | |
@start_button.set_action_command("start") | |
@start_button.add_action_listener do |evt1| | |
@start_button.set_enabled(false) | |
self.set_cursor(Cursor.get_predefined_cursor(Cursor::WAIT_CURSOR)) | |
# Instances of javax.swing.SwingWorker are not reusuable, so | |
# we create new instances as needed. | |
task = Task.new(self, @start_button, @task_output) | |
task.add_property_change_listener do |evt2| | |
if "progress" == evt2.get_property_name | |
progress = evt2.get_new_value.to_i | |
@progress_bar.set_value(progress) | |
@task_output.append("Completed #{progress}% of task.\n"); | |
end | |
end | |
task.execute | |
end | |
@task_output = javax.swing::JTextArea.new(5, 20) | |
@task_output.set_margin(Insets.new(5,5,5,5)) | |
@task_output.set_editable(false) | |
@progress_bar = JProgressBar.new(0, 100) | |
@progress_bar.set_value(0) | |
@progress_bar.set_string_painted(true) | |
@panel = javax.swing::JPanel.new | |
@panel.add(@start_button) | |
@panel.add(@progress_bar) | |
add(@panel, BorderLayout::PAGE_START); | |
add(JScrollPane.new(@task_output), BorderLayout::CENTER) | |
setBorder(BorderFactory.create_empty_border(20, 20, 20, 20)) | |
end | |
end | |
javax.swing::SwingUtilities.invoke_later do | |
ProgressBarDemo.create_and_show_gui | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment