-
Notifications
You must be signed in to change notification settings - Fork 0
SWT JRuby bootstrap guide
The Standard Widget Toolkit (SWT) is a free and open-source graphical library written in Java. It provides native implementation of most windowing widgets, and for this reason, performs better than other Java libraries like AWT or Swing. A famous project using SWT for its Graphical User Interface is Eclipse.
With JRuby, we don't need to code in Java to get the benefits of portable, yet native cross-platform GUI. We can use Ruby. This little guide intend to bootstrap you at SWT programing with JRuby.
This guide uses JRuby 1.6.7.2
In order to use SWT, you need to require it. An easy way to do it is to install the swt gem, created by danlucraft.
$ gem install swt
Before going further, let's make sure the system is working correctly. To do that, fire-up your favorite editor and try to require java and swt. Let's call that file testing_swt.rb
require 'java'
require 'swt'
You need to execute it with JRuby and rubygems. Also, if you are using Mac OS, you need to set the -J-XstartOnFirstThread flag.
$ jruby -J-XstartOnFirstThread -rrubygems testing_swt.rb
At that point, you shouldn't see anything happening: it means that it's working!
The way an SWT program work is like this: there's an event loop that waits for an event; it dispatches that event to the appropriate handler or it sleeps.
In SWT parlance, that object responsible of waiting and dispatching events is called a Display. Each SWT program needs a display.
require 'java'
require 'swt'
# Each Swt application need a display to interact with the operating system.
display = Swt::Widgets::Display.new
So now, we still don't have any window. Let's create one! In SWT, a window is called a Shell. There can be as much shells as you want to. In order to create one, you need to associate it with a display.
# Let's create a new window. It's called "Shell" in Swt vocabulary.
# Note that we pass it a display.
shell = Swt::Widgets::Shell.new(display)
Nice. Before adding any widget inside, we need to talk about Layouts. In SWT, items are added to elements that are able to carry other elements, such as 'Composite', or 'Group'. Those elements are called 'Containers'. Your shell is a container: you can add items to it.
In SWT, a Layout Manager is responsible for deciding how items are added to a container. There are various Layout managers, and you can find the complete list in the SWT API.
What we want to add to our shell is a Label and a Button. Let's use the GridLayout with two column to do that. As its name implies, the GridLayout fraction your shell in a grid.
# Each shell require a layout. It's there to organize the widgets in the shell.
layout = Swt::Layout::GridLayout.new(2, false)
shell.setLayout(layout)
Next, we add the Label and the button:
# Let's create a label and add it to the parent container (the shell)
label = Swt::Widgets::Label.new(shell, Swt::SWT::NONE)
label.text = "This is a label"
button = Swt::Widgets::Button.new(shell, Swt::SWT::PUSH)
button.text = "Click Me"
As you can see, the parent of the element (its container) is passed to the element in the constructor. The second argument is the style.
What would be cool would be to change the content of the label once the button is clicked. We can add a listener to the button to do that.
button.add_selection_listener do
label.text = "clicked!"
end
Now let's display the shell:
# Ready to display the shell!
shell.open
Great! But as it stands, the program won't run... Or at least, it wouldn't stay on screen for long, since there is no infinit loop keeping it on. We need to take care of this.
# We need an event loop now.
while !shell.isDisposed
# The application sleeps unless there's a new event.
display.sleep unless display.read_and_dispatch
end
display.dispose
Now, execute this with a jruby -J-XstartOnFirstThread -rrubygems
and voilà!
For the complete source code, here is the gist.
swt gem: https://github.com/danlucraft/swt
Great tutorial (Java): http://www.vogella.com/articles/SWT/article.html#layout