Skip to content

Instantly share code, notes, and snippets.

@migane
Created September 24, 2012 13:46
Show Gist options
  • Save migane/3776032 to your computer and use it in GitHub Desktop.
Save migane/3776032 to your computer and use it in GitHub Desktop.
Login window with MigLayout in JRuby
# Using MigLayout as frankly Javax Swing without a decent GUI
# tool is really a nightmare
# One has to download the MigLayout jar from its site
# then put it inside the jruby/lib folder
require 'java'
# For rvm users => allow to just run jruby frontend.rb
# provided that a .rvmrc has been created in the directory where
# frontend.rb is located.
# If not, use rvm_path the same way as jruby_path with ENV['rvm_path']
# One could also without rvm define a jruby_path in his environment.
# And without that it should be run as j
# ruby -J-cp /pathtojruby/lib/miglayout-4.0.jar frontend.rb
jruby_path = File.expand_path(ENV['MY_RUBY_HOME'] || '~/.rvm/rubies/jruby-1.7.0.preview2')
require "#{jruby_path}/lib/miglayout-4.0.jar"
# Loading the classes using ruby, much less tiring than listing all the imports
swingClasses = %w(JFrame JPanel
BorderFactory JLabel JTextField JPasswordField
JButton SwingConstants)
awtClasses = %w(Color EventQueue event.ActionListener event.ActionEvent)
swingClasses.each {|swc| java_import "javax.swing.#{swc}"}
awtClasses.each {|awc| java_import "java.awt.#{awc}"}
java_import 'net.miginfocom.swing.MigLayout'
# Far better with reopening JButton and passing a block with parameters
class JButton
def on_clicked login,password,message, &block
add_action_listener do |e|
block.call
end
end
end
# Not sure if it is not dangerous here,
# maybe should be in the block passed to JButton
class JPasswordField
def get_as_string
clear_password= ''
get_password.each { |ch| clear_password << ch }
return clear_password
end
end
# I confess I install Eclipse to Get WindowBuilder and write first
# the code in Java, and then manually translate it into JRuby
class MyFrontEnd < JFrame
def initialize(title)
super(title)
set_default_close_operation JFrame::EXIT_ON_CLOSE
contentPane = JPanel.new
contentPane.border = BorderFactory.createEmptyBorder 5,5,5,5
set_content_pane contentPane
contentPane.layout = MigLayout.new "",
"[93.00:89.00][][137.00][128.00:128.00][122.00]", "[25.00][][36.00][:25.00:29.00][38.00,center]"
#{#}"[93.00:89.00][][137.00][128.00:128.00][122.00]", "[25.00][][36.00][:25.00:29.00][38.00,center]"
frameTitle = JLabel.new 'A Simple Application'
frameTitle.horizontal_alignment = SwingConstants::CENTER
contentPane.add frameTitle, 'cell 2 0 2 1,alignx center,aligny top'
loginLabel = JLabel.new 'Loginid'
contentPane.add loginLabel, 'cell 0 1'
loginField = JTextField.new
contentPane.add loginField, 'cell 3 1'
loginField.columns = 10
passwordLabel = JLabel.new 'Password'
contentPane.add passwordLabel, 'cell 0 2'
passwordField = JPasswordField.new
passwordField.columns = 10
contentPane.add passwordField, 'cell 3 2'
messageLabel = JLabel.new 'Message'
contentPane.add messageLabel, 'cell 0 3'
messageField = JTextField.new
messageField.columns = 25
messageField.opaque = true
messageField.background = Java::Java.awt.Color.new 237, 237, 237
messageField.border = BorderFactory.createCompoundBorder \
BorderFactory.createLineBorder(Color.new(133, 170, 220), 2),
BorderFactory.createEmptyBorder(2, 5, 2, 5)
messageField.enabled = false
messageField.editable = false
messageField.focusable = false
contentPane.add messageField, 'cell 3 3 2 1'
submitButton = JButton.new 'Submit'
submitButton.on_clicked(loginField, passwordField, messageField) {
# Tribute to Satish who makes me work hard :-)
if loginField.get_text == "Satish" && passwordField.get_as_string == "Talim"
messageField.set_text "Congratulations, you are now logged in!"
else
messageField.set_text "Sorry, incorrect login or password"
end
}
contentPane.add submitButton, 'flowx,cell 2 4,alignx right,aligny bottom'
refreshButton = JButton.new 'Refresh'
refreshButton.on_clicked(loginField, passwordField, messageField) {
loginField.set_text ""
passwordField.set_text ""
messageField.set_text ""
}
contentPane.add refreshButton, 'cell 3 4,alignx left,aligny bottom'
pack
set_visible true
end
end
mb = MyFrontEnd.new('Assignment 1')
@kotp
Copy link

kotp commented Sep 24, 2012

Alternatively, can you pass the user object to your MyFrontEnd?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment