uros calakovic
uros calakovic

Reputation: 277

Load an FXML form using JRuby

I am trying to load an FXML form from a JRuby script. I can successfully do that from a Jython script. This is the basic Jython code:

from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.scene import Scene


class Main(Application):

    def start(self, stage):

        main_form = self.getClass().getResource('/main.fxml')
        self.loader = FXMLLoader(main_form)
        root = self.loader.load()

        scene = Scene(root)

        stage.setScene(scene)
        stage.show()


args = []

print Main().class
print args

Application.launch(Main().class, args)

The script executes fine and the form gets shown. The output of the script is:

<type 'org.python.proxies.__main__$Main$0'>
[]

So Main.class() is type 'org.python.proxies.main$Main$0'

This is my attempt to create the equivalent JRuby script:

java_import javafx.application.Application
java_import javafx.fxml.FXMLLoader
java_import javafx.scene.Scene


class Main < Application

  attr_accessor :loader, :fxml_form, :scene

  def start(stage)

    @fxml_form = self.java_class.getResource('/main.fxml')
    @loader = FXMLLoader.new(@fxml_form)
    root = @loader.load()

    @scene = Scene.new(root)
    stage.set_scene(@scene)
    stage.show()

  end

end

args = Array.new()

puts Main.new().java_class
puts args

Application.launch(Main.new().java_class, args)

The output of this script is:

javafx.application.Application
NameError: no method 'launch' for arguments (org.jruby.javasupport.JavaClass,org.jruby.RubyArray) on Java::JavafxApplication::Application
  available overloads:
    (java.lang.String[])
    (java.lang.Class,java.lang.String[])
  (root) at /home/uros/NetBeansProjects/JRuby-JavaXF/lib/main.rb:31

So Main.new().java_class is javafx.application.Application and the form never gets loaded. My FXML in both cases is a basic 'hello world' form:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


    <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="264.0" prefWidth="315.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <Button mnemonicParsing="false" prefHeight="34.0" prefWidth="89.0" text="Button" />
       </children>
    </StackPane>

I am on Linux Mint 17. My Jython is 2.7b3 and my JRuby is 1.7.16.1.

[EDIT]

Additional information:

When trying to use the jrubyfx gem like this:

require 'jrubyfx'

class Main < JRubyFX::Application

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

end

Main.launch

I get the following exception:

NameError: cannot initialize Java class javafx.scene.control.ListView
    (root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/core_ext/precompiled.rb:320
   require at org/jruby/RubyKernel.java:1065
    (root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1
   require at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55
      glob at org/jruby/RubyDir.java:242
  load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:313
  load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:312
   require at org/jruby/RubyKernel.java:1065
    (root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx.rb:37
    (root) at /home/uros/NetBeansProjects/RubyApplication4/lib/main.rb:1

My Java version:

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Server VM (build 25.25-b02, mixed mode)

Upvotes: 0

Views: 330

Answers (1)

byteit101
byteit101

Reputation: 4010

NameError: cannot initialize Java class javafx.scene.control.ListView

was caused by

Java(TM) SE Runtime Environment (build 1.8.0_25-b17)

That JRubyFX error was fixed in 1.1.1 (which added java 8 support). 1.1.0 only supported Java 7 (after u6). It should work now using the code you gave in both 7u6+ and 8:

require 'jrubyfx'

class Main < JRubyFX::Application
  def start(stage)
      with(stage, title: "Hello World!", width: 800, height: 600) do
        fxml "main.fxml"
        show
      end
  end
end

Main.launch

Upvotes: 2

Related Questions