[jruby-user] JRubyFX

18 views
Skip to first unread message

Rodrigo Botafogo

unread,
Apr 29, 2015, 12:45:20 PM4/29/15
to us...@jruby.codehaus.org
Hello.... 

Sorry if this isn´t the right place to post questions about JRubyFX.  

I´m trying to write a JRubyFX application.  I´m following a post from Oracle to try to learn JavaFX and JRubyFx.  In the post, in a js file there is the following lines:

 // Complete initialization when page is loaded.
  This.engine.loadWorker.stateProperty().addListener(new ChangeListener() {

ChangeListener() is not found when I run the application.  I understand that I should load javafx/beans, but I don´t know how to do this with JRubyFX.  Can anybody help me here or point to the right list to ask this question?

Thanks
--
Rodrigo Botafogo



Thomas E Enebo

unread,
Apr 29, 2015, 2:21:27 PM4/29/15
to jruby-user
javafx.beans.value.ChangeListener is an interface so with implicit closure coercion you should be able to:


```ruby
This.engine.load_worker.state_property.add_listener do |observable, old_value, new_value|
  #...
end
```

-Tom

--
blog: http://blog.enebo.com       twitter: tom_enebo
mail: tom....@gmail.com

Rodrigo Botafogo

unread,
Apr 29, 2015, 2:33:21 PM4/29/15
to us...@jruby.codehaus.org
Hi Tom,

Not sure I follow... so, the following function is defined inside a javascript file and not in a JRuby script.  I can´t do this implicit coercion.  I think I should load the ChangeListener interface and make it available to javascript.  I´m still learning all this, so I might just be confused.  If this is any help, I´m just trying to follow the blog: https://blogs.oracle.com/nashorn/entry/porting_from_the_browser_to, but instead of using jjs, I´m driving nashorn through JRuby.


function WebViewWrapper(onload) {
    var This = this;
    var WebView = Java.type("javafx.scene.web.WebView");
    var webview = new WebView();
    
    This.webview = webview;
    This.engine = webview.engine;
    This.window = undefined;
    This.document = undefined;
    
    // Make sure the JavaScript is enabled.
    This.engine.javaScriptEnabled = true;
    
    // Complete initialization when page is loaded.
    This.engine.loadWorker.stateProperty().addListener(new ChangeListener() {
        changed: function(value, oldState, newState) {
            if (newState == Worker.State.SUCCEEDED) {
                This.document = wrap(This.engine.executeScript("document"));
                This.window = wrap(This.engine.executeScript("window"));
                
                // Call users onload function.
                if (onload) {
                    onload(This);
                }
            }
        }
    });


Thanks....
--
Rodrigo Botafogo



Thomas E Enebo

unread,
Apr 29, 2015, 2:37:36 PM4/29/15
to jruby-user
I am less of a help for JS but if you see how WebView is defined above you can perhaps do he same thing for ChangeListener is your JS code is barfing because it does not know what that type is:

var ChangeListener = Java.type("javafx.beans.value.ChangeListener");

-Tom

Rodrigo Botafogo

unread,
Apr 29, 2015, 2:52:23 PM4/29/15
to us...@jruby.codehaus.org
Tom,

No, this doesn´t work.  Could I do a require in my Ruby code somehow? 

require 'javafx.beans.value.ChangeListener' does not work as it is expecting a jar file, right?  Where is the jar for ChangeListener?


Thanks,
--
Rodrigo Botafogo



Christian MICHON

unread,
Apr 30, 2015, 2:51:20 AM4/30/15
to us...@jruby.codehaus.org
Maybe I can help out.

I've been toying for months now with Nashorn (jjs) but when I use Nashorn, I only use jjs and do not embed the JS engine inside another java-based interpreter.

1st: make sure your JDK8 is complete. It should not be the server JRE but the full JDK8.

2nd: to confirm javafx is included in your environment, try the following lines in jjs.

If javafx is not in your JDK8, you'll see this:

jjs> Java.type('javafx.beans.value.ChangeListener')
java.lang.RuntimeException: java.lang.ClassNotFoundException: javafx.beans.value.ChangeListener

In that case, it'll never work in jruby and you need to reinstall JDK8 properly.

This is how it should look like in jjs if javafx is included in your environment:

jjs> Java.type('javafx.beans.value.ChangeListener')
[JavaClass javafx.beans.value.ChangeListener]

Once this is ok, you just need to add the following line inside your ruby script (preferably after require 'java') :

include_class Java::JavafxBeansValue::ChangeListener

Good luck!
--
Christian

Christian MICHON

unread,
Apr 30, 2015, 2:56:32 AM4/30/15
to us...@jruby.codehaus.org
In case you still wish to require or have a look at the jar file containing the class ChangeListener, it's $JAVA_HOME/jre/lib/ext/jfxrt.jar
--
Christian

Rodrigo Botafogo

unread,
Apr 30, 2015, 3:42:21 PM4/30/15
to us...@jruby.codehaus.org
Hi Christian,

Thanks to your help and the others I managed to get things going.  Basically, I had to add to the LOAD_PATH << .../jre/lib/ext and then on the javascript file do: 

load("fx:base.js")
load("fx:controls.js");
load("fx:graphics.js");
load("fx:web.js");

Thanks....


--
Rodrigo Botafogo



Reply all
Reply to author
Forward
0 new messages