[JRuby] Ruby and Swing: Profligacy

  • Thread starter Charles Oliver Nutter
  • Start date
C

Charles Oliver Nutter

Profligacy is a JRuby/Swing framework created by Zed Shaw. It takes a
different approach from the previously posted Cheri, in that it doesn't
try to hide the Swing API. Instead, it just provides solutions for some
of the ugliest parts of Swing like component layout.

http://ihate.rubyforge.org/profligacy/

From the page:

"Profligacy is a JRuby library that makes building Swing Graphical User
Interface much easier than with Raw code. It’s not a builder as with
many other projects, but instead a simple Ruby way to structure the UI
for the 80% common cases you’ll encounter."

"The purpose of Profligacy is not to be a complete way of hiding Swing
components from you. You’ll still be making JButtons and JLabels, you’ll
just be putting them into a Ruby idiomatic code structure that doesn’t
make your eyes hemorrhage diarrhea like when you try to code in Java."

It's a much lighter-weight approach to doing Swing GUI development. The
most interesting bit, however, is the layout engine:

The only real innovation in Profligacy is a simpler way to configure a
GroupLayout using a simple regex/wiki style syntax. This is a work in
progress, but it should make building GUIs much much easier.

Here's a medium-sized sample of building a simple GUI with layout in
Profligacy:

<code swing8_lel.rb>
require 'profligacy/swing'
require 'profligacy/lel'

class PhoneBookDemo
include_package 'javax.swing'
include_package 'java.awt'
include_package 'javax.swing.border'
include Profligacy

def initialize
@search_style = :exact

layout =
"[>lab_name][*name][>lab_number][*number][*lab_options][<exact][<starts][<ends]"


@ui = Swing::LEL.new(JFrame, layout) do |c,i|
c.lab_name = JLabel.new "Name"
c.lab_number = JLabel.new "Number"
c.lab_options = JLabel.new "Search Options"
c.name = JTextField.new 10
c.number = JTextField.new 10

c.exact = JRadioButton.new("Exact Match", true)
c.starts = JRadioButton.new("Starts With")
c.ends = JRadioButton.new("Ends With")

b = ButtonGroup.new
[c.exact, c.starts, c.ends].each {|x| b.add(x) }

i.name = {:action => proc {|t,e| @ui.number.text = find(@names,
e.source.text) } }
i.number = {:action => proc {|t,e| @ui.name.text = find(@numbers,
e.source.text) } }
i.exact = {:action => proc {|t,e| @search_style = :exact } }
i.starts = {:action => proc {|t,e| @search_style = :starts } }
i.ends = {:action => proc {|t,e| @search_style = :ends } }
end

@ui.build:)args => "Phone Book")

@names = {
"Zed A. Shaw" => "917-555-5555",
"Frank Blank" => "212-554-5555"
}

@numbers = {
"917-555-5555" => "Zed A. Shaw",
"212-554-5555" => "Frank Blank"
}
end

protected
def find(inside, text)
results = case @search_style
when :exact; inside.keys.grep /^#{text}$/
when :starts; inside.keys.grep /^#{text}/
when :ends; inside.keys.grep /#{text}$/
end

inside[results[0]] || ""
end
end

SwingUtilities.invoke_later proc { PhoneBookDemo.new }.to_runnable
</code>

- Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top