Swing with a JRuby Wrapper: Profligacy

recycle icon

I recently whined about the dearth of sophisticated GUI builders in the Rubyverse. I still think that there’s a gap in the market for a slick product with a visual interface builder that surpasses the current crop to exploit. On the other hand, if you just want to create modest GUI things with Ruby, maybe a drag n’ drop, wysiwyg IDE with extra code-completion sauce could be deemed a tad unnecessary.

A while ago I bookmarked a site whilst perusing the works of one Zed A. Shaw (the biological father of Mongrel). I forgot about it until casting my net into the Omniscient Online Ocean to trawl for Ruby GUI fish. What I snared was a reference to Profligacy, a JRuby library that helps you to put some soothing Ruby mojo into the Swing GUI building maelstrom.

I’ve really tried to enjoy creating desktop applications with Swing, sadly, I remain less than enthusiastic about drowning in a pool of unadulterated Java and Swing. I just find it intellectually painful. Swing reminds me of Cod Liver Oil; I know that it’s good stuff, but it tastes a whole lot more pleasant with orange flavouring. Profligacy is to Swing what orange flavouring is to Cod Liver Oil.

To use Profligacy you’re going to need -

  1. Java 1.5+
  2. JRuby
  3. the Profligacy library (I recommend installing the gem via gem install profligacy)

Trying to convince you of the power of Profligacy with paragraphs of pushy prose probably would not prove particularly persuasive, so ’ere’s some code.

profello.rb – A Hello World program using Profligacy

   1  # profello.rb 
   2  require 'rubygems' 
   3  require 'profligacy/swing'
   4  require 'profligacy/lel'
   5  
   6  class TestWindow
   7    include_package 'javax.swing'
   8    include_package 'java.awt'
   9    include Profligacy
  10  
  11    def initialize
  12      ui = Swing::LEL.new JFrame, "[*lab][buttons]" do |c,i|
  13        c.lab = JLabel.new "Press A Button."
  14        c.lab.preferred_size = Dimension.new(300, 0)
  15  
  16        c.buttons = Swing::LEL.new JPanel, "[first | second]" do |d,j|
  17          d.first = JButton.new "First" 
  18          d.second = JButton.new "Second"
  19          j.first = { :action => proc {|t,e| c.lab.text = "Hello world!" } }
  20          j.second = { :action => proc {|t,e| c.lab.text = "Are you ready for some GUI Profligacy?" } }
  21        end.build :auto_create_container_gaps => false
  22      end
  23  
  24      ui.build(:args => "Hello World") do |c|
  25        c.location_relative_to = nil
  26        c.default_close_operation = JFrame::EXIT_ON_CLOSE
  27      end
  28    end
  29  end
  30  
  31  SwingUtilities.invoke_later proc { TestWindow.new }.to_runnable

Run it -

jruby profello.rb

screenshot from running profello.rb

screenshot

I’m assuming that you installed the Profligacy gem. If you didn’t, then some minor amendments to the code above will probably be required.

Java + JRuby is a winning combination for cross-platform GUI creations. Profligacy eases the pain.

Zed, if you are ever in the U.K. near the badlands of Essex, there is a pint of warm British beer waiting for you.

Permalink | comments(View)

blog comments powered by Disqus