Skip to content

Instantly share code, notes, and snippets.

@enebo
Created December 23, 2010 19:52
Show Gist options
  • Save enebo/753457 to your computer and use it in GitHub Desktop.
Save enebo/753457 to your computer and use it in GitHub Desktop.
require 'java'
java_import java.awt.BorderLayout
java_import java.awt.Container
java_import java.awt.GridLayout
java_import java.awt.event.ActionEvent
java_import java.awt.event.ActionListener
java_import javax.swing.JButton
java_import javax.swing.JFileChooser
java_import javax.swing.JFrame
java_import javax.swing.JPanel
java_import javax.swing.JTextField
#### Could use some OO love but it should show basics of using closures
#### to define action listeners and other handy Ruby short-hand syntax
frame = JFrame.new
panel = JPanel.new
open = JButton.new "Open"
save = JButton.new "Save"
filename = JTextField.new
dir = JTextField.new
open.addActionListener do |event|
c = JFileChooser.new
# Demonstrate "Open" dialog:
rVal = c.showOpenDialog frame
if rVal == JFileChooser::APPROVE_OPTION
filename.text = c.selected_file.name
dir.text = c.current_directory.to_string
end
if rVal == JFileChooser::CANCEL_OPTION
filename.text = "You pressed cancel"
dir.text = ""
end
end
panel.add open
save.addActionListener do |event|
c = JFileChooser.new
# Demonstrate "Save" dialog:
rVal = c.showSaveDialog frame
if rVal == JFileChooser::APPROVE_OPTION
filename.text = c.selected_file.name
dir.text = c.current_directory.to_string
end
if rVal == JFileChooser::CANCEL_OPTION
filename.text = "You pressed cancel"
dir.text = ""
end
end
panel.add save
cp = frame.content_pane
cp.add panel, BorderLayout::SOUTH
dir.editable = false
filename.editable = false
button_panel = JPanel.new
button_panel.layout = GridLayout.new(2, 1)
button_panel.add filename
button_panel.add dir
cp.add button_panel, BorderLayout::NORTH
frame.default_close_operation = JFrame::EXIT_ON_CLOSE
frame.set_size 250, 110
frame.visible = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment