# Ruby and Swing: Profligacy

**URL:** <https://www.ruby-forum.com/t/ruby-and-swing-profligacy/118865>\
**Category:** Ruby\
**Created:** [October 31, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/ruby-and-swing-profligacy/118865 "2007-10-31T19:17:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 31, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/ruby-and-swing-profligacy/118865/1 "2007-10-31T19:17:43Z")

</div>

Profligacy is a JRuby/Swing framework created by Zed S… 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/](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:

`
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
`

- Charlie
