Sunday, March 20, 2016

Playing with JRubyFX

I started looking at JRubyFX as a combination of using a proven UI solution with Ruby as the programming language and I have to admit that I am impressed with how little it takes to get a UI done -- especially when it is driven by FXML.

Unfortunately the learning curve is steep, as one has to learn JavaFX and the special way JRubyFX calls the JavaFX classes and methods. And unfortunately, Google is not returning too much results here.

Anyway: this little code already creates GUI and populates a TreeView with elements it pulls from a remote server.

require 'jrubyfx'
require 'hawkular_all'

fxml_root File.dirname(__FILE__)

class HawkFx < JRubyFX::Application

  def start(stage)
    with(stage, title: 'Hello World!', width: 800, height: 600) do
      fxml HawkFxController
      show
    end

    stage.show()
  end
end

class HawkFxController
  include JRubyFX::Controller
  fxml 'fxmain.fxml'

  def login # callback from the login button
    creds = { :username => 'jdoe' ,
              :password => 'password' }
    url = 'http://localhost:8080/hawkular/inventory'
    @inventory_client = Hawkular::Inventory::InventoryClient.new(url, creds)
    @tenant = @inventory_client.get_tenant
    @FXMLtextArea.text = "Tenant: #{@tenant}"
    feeds = @inventory_client.list_feeds

    show_initial_tree(feeds)

  end

  def show_initial_tree(feeds)
    tree_root = tree_item('Feeds') do
      feeds.each do |feed|
        item = tree_item(feed) # this already adds the item to the root
      end
    end
    # bind to the view from fxml
    @FXMLtreeView.setRoot(tree_root)

    tree_root.setExpanded true
  end
end

HawkFx.launch

3 comments:

Unknown said...

Is JavaFX really proven yet? I can't even get it to render text correctly. Not even in WebView, even though it's supposed to be using WebKit, and every other WebKit application seems to have no trouble.

Heiko Rupp said...

There are a lot of folks that do large projects with JavaFX successfully, so I'd say yes it is proven.

Unknown said...

I haven't seen one working example yet.

I tried using Label to render text and emoji came out as horizontal black bars.

So I gave up on Label and tried using WebView. Unfortunately WebView has some issue where it can't figure out its own vertical height correctly, though, so I ended up finding a hack where someone had used JS from within the page itself to figure out the height, and then set it back on the parent. So now it knows its own height, but emoji comes out as black diamonds with question marks. On top of that, straight Japanese characters occasionally came out as empty squares, until you selected the text, at which point the characters appeared.

I expected a modern toolkit to cope with relatively modern issues, such as non-ASCII text, but JavaFX completely fails at it.

The only way I could see someone using JavaFX "successfully" is if they never have to render text. Until it can do that correctly, it isn't even an acceptable solution, let alone a "proven" one.