JavaFX has been getting a lot of attention lately, and at least since JavaFX 2.0 it has been possible to use it from JRuby. But there is an awesome JRuby library you should know about, which makes working with JavaFX cleaner, easier and far more "rubyish": JRubyFX. It’s easy to get started with, and comes with a number of conveniences, such as tools to easily create executable jars and even package your app as a native installer, and even a ruby file generator that will inspect FXML documents (more on FXML in a bit) and create a stubbed out
.rb file, with all fx_id and fx_handler references ready for you to use.
First of all, if you want to be able to use all the packaging capabilities of JRubyFX and create native installers, you’ll need to be running JDK8, for everything else, using Oracle’s JDK7 should suffice. As far as I know, OpenJDK doesn’t contain the JavaFX libraries yet, as of February 3, 2013. Look for this to change in the near future.
Next, you’ll need to get a copy of JRubyFX. You can get this in one of two ways. Either:
gem install jrubyfx
Or, if you want all the convenience utilities and latest updates:
git clone https://github.com/jruby/jrubyfx.git cd jrubyfx jruby -S rake install
Whichever way you go, once its installed and available as a gem, you should now be able to
require "jrubyfx" and use it from your programs. There is a nice Getting Started guide on the JRubyFX github page which I recommend you give a quick read. Especially if you are already familiar with JavaFX, this may be all you need to get going.
JRubyFX gives us many ways to work with the JavaFX classes. We can use them in a way which is pretty much a straightforward translation of Java to Ruby, like:
label = Label.new()
There’s the builder pattern:
label = build(Label, text: "Hello World")
There’s the DSL, which calls the constructor and passes it the arguments. You can also pass it a hash or a block:
You can read more about all three of these in the previously mentioned Getting Started guide. They are all three useful. The DSL is definitely preferable to the other two, as it saves significant typing, and seems to be more stylistically Ruby. However, I seem to have come upon situations, in my experimentation with it, where the DSL is too ambigious, and it’s necessary to fall back on the more straightforward method. This could, however, merely be my own lack of experience with the Ruby language, especially when it comes to using Ruby to interact with Java.
What I’m personally most interested in, however, is using JRubyFX with FXML. FXML is an XML dialect used to describe the UI of JavaFX applications. You can then write the code in Ruby which tells it what to do. Most of my previous programming experience has been in web development, so this feels very similar to using a Model, View, Controller pattern, like in Rails.
If you are already rolling your eyes, thinking of Visual Design tools and WYSIWYG layout editors of the past, you really should give SceneBuilder a shot first. I think they’ve learned from the mistakes those earlier tools made, and have instead created something that’s truly pleasant to use. This also makes it easy for people to work in teams, a designer or UX expert could put together the visual experience of your application, working directly in SceneBuilder.
One of the happiest surprises, for me, was that you can style the UI using CSS. While the properties and classes are JavaFX specific, the CSS is based on the W3C specification and works just like with HTML. While I’m no designer, and make no claims to having any CSS ability, this is familiar and useful to me. You create your stylesheet, then you can attach it to the UI directly in SceneBuilder. How this is accomplished might seem a little confusing, so I reccomend the following tutorial: JavaFX Tutorial Part IV: CSS Styling. As is obvious from the "Part IV", this is only one part in a much longer tutorial series, and the entire thing is an excellent introduction to JavaFX if you don’t know your way around yet. This can also be done programatically, though I have not personally tried doing it this way yet.
If you are a Ruby programmer looking for a way to code either a desktop GUI or Rich Internet Application, or a JavaFX programmer looking for a more powerful, expressive language, I strongly reccomend giving JRubyFX a shot. I plan to put together more material soon, with a few more details on working with this incredible library soon. In the meanwhile, check out the project, the Getting Started, and spend some time playing around with SceneBuilder. Enjoy!