Swinging with JRuby
So far we've done some quaint examples, but nothing large. We will take the code from this page and produce the same results with Ruby.
Some of the changes that are needed:
- converting new <Object>(par1,par2) syntax into <Object>.new(par1,par2)
- accessing fields with GridBagConstraints::HORIZONTAL syntax instead of GridBagConstraints.HORIZONTAL
- removing the old array used to construct the JComboBox:
since this will try to populate the combo box with a Ruby object and the JComboBox will not like that at all. To get around this we had the items one at a time:String[] options = {"all", "any"}; modeCombo = new JComboBox(options);modeCombo = JComboBox.new modeCombo.addItem "all" modeCombo.addItem "any" - The typical verbose way in Java of exiting the program when the windows closes goes from:
to:addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });window.defaultCloseOperation = JFrame::EXIT_ON_CLOSE - main method is removed
The full code is:
require 'java'
include_class(['javax.swing.JFrame','javax.swing.JLabel','javax.swing.JComboBox','javax.swing.JButton','javax.swing.JPanel','javax.swing.JTable','javax.swing.JTextField', 'java.awt.GridBagLayout', 'java.awt.GridBagConstraints'])
window= JFrame.new
contentPane = window.getContentPane
gridbag = GridBagLayout.new
contentPane.setLayout gridbag
c = GridBagConstraints.new
#setting a default constraint value
c.fill = GridBagConstraints::HORIZONTAL
tagLbl = JLabel.new("Tags")
c.gridx = 0
c.gridy = 0
gridbag.setConstraints tagLbl, c
contentPane.add(tagLbl)
tagModeLbl = JLabel.new("Tag Mode")
c.gridx = 0
c.gridy = 1
gridbag.setConstraints(tagModeLbl, c)
contentPane.add(tagModeLbl)
tagTxt = JTextField.new("plinth")
c.gridx = 1
c.gridy = 0
c.gridwidth = 2
gridbag.setConstraints(tagTxt, c)
contentPane.add(tagTxt)
modeCombo = JComboBox.new
modeCombo.addItem "all"
modeCombo.addItem "any"
c.gridx = 1
c.gridy = 1
c.gridwidth = 1
gridbag.setConstraints(modeCombo, c)
contentPane.add(modeCombo)
searchBtn = JButton.new("Search")
c.gridx = 1
c.gridy = 2
gridbag.setConstraints(searchBtn, c)
contentPane.add(searchBtn)
resTable = JTable.new(5,3)
c.gridx = 0
c.gridy = 3
c.gridwidth = 3
gridbag.setConstraints(resTable, c)
contentPane.add(resTable)
previewLbl = JLabel.new("Preview goes here")
c.gridx = 0
c.gridy = 4
gridbag.setConstraints(previewLbl, c)
contentPane.add(previewLbl)
window.defaultCloseOperation = JFrame::EXIT_ON_CLOSE
window.pack()
window.setVisible(true)
If you create a file from this listing, you can invoke it in JRuby like so:
jruby testwin.rb
Superficially, there is no difference between our JRuby and Java versions of code. Besides the obvious hit to performance incurred by using JRuby, the upside is that it is far quicker to create the code with the same functionality.
It will be interesting to see the speed improvements that occur when JRuby gets a JIT compiler, but in the meantime, if you do not care about performance, you can certainly take JRuby for a spin and see what can be done.
Do you need help with Java, C, or C++? 





1
Gavri Fernandez - 08/11/07
"just to keep it interesting, the following also works:"
That it does isn't specific to JRuby. I'm not saying that you're saying it is. But it's kind of misleading.
:: and . are different only by convention. You can use :: to access instance methods and . to access class constants, if you want to.
» Report offensive content
2
J. V. - 08/04/09
Instead of this:
modeCombo = JComboBox.new
modeCombo.addItem "all"
modeCombo.addItem "any"
You should use "to_java" method on ruby array:
modeCombo = JComboBox.new ["all", "any"].to_java
The same thing must be used for JList, where is no add_item method, and "to_java" is the only one way to populate JList.
Here you can find complete examples for both swing components: www.readytouseexamples.com
» Report offensive content