Integrating JavaFX and Java

I’m just starting to learn how to use JavaFX, a new contender in the Rich Internet Application arena. In a nutshell, JavaFX is a Java-based technology that seeks to compete with platforms such as Adobe Flash and Microsoft Silverlight by enabling development for desktop-based applications, browser-based applications and mobile phones. One of the nicer aspects of JavaFX is that it easily integrates with Java libraries — you can reuse existing code or develop parts of your application in Java. This way, you can take advantage of the strengths of JavaFX script for graphics manipulation, GUI, and access to web services while continuing to benefit from Java.

Now, it seems clear enough to me how to create and use Java objects from within JavaFX script — basically, import them and treat them like JavaFX objects. However, I got curious about whether it was possible to call JavaFX from Java; happily, it seems that you can. I don’t know that you SHOULD, but you can.

Here’s a chunk of my small test application for JavaFX on a mobile platform. I want to pull and process a web page using a Java object, but one of the things I’ve noticed is that JavaME offers a much smaller library than the full Java API. Fortunately, JavaFX has some great HTTP routines built in, so I wondered if I should try calling them from Java as part of an object designed to import data…

package x;
 
import javafx.io.http.*;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
 
public class HttpTest {
 
    public void go() {
        HttpRequest hr = new HttpRequest();
 
        hr.$location.set("http://www.epsilon11.com/");
 
        // onDoneRead() function definition
 
        com.sun.javafx.functions.Function0 fDoneRead =
                new com.sun.javafx.functions.Function0() {
 
                    @Override
                    public Void invoke() {
 
                        System.out.println("onDoneRead() called");
 
                        return null;
                    }
                };
 
 
        // onInput() function definition
 
        com.sun.javafx.functions.Function1 fInput =
                new com.sun.javafx.functions.Function1() {
 
                    @Override
                    public Void invoke(java.lang.Object param1) {
                        InputStream is = (InputStream) param1;
 
                        System.out.println("onInput() called");
 
                        String st = "";
 
                        // Read the fetched webpage from the input stream (is)
                        // into the string (st).  I'm using a 1024-byte buffer,
                        // but that can obviously be changed.
 
                        try {
                            int iRead = 0;
 
                            do {
                                byte[] byText = new byte[1024];
 
                                // Read only as much of the input stream as
                                // is available (it will block otherwise...)
 
                                iRead = (is.available() > 1024 ? 1024 : is.available());
 
                                iRead = is.read(byText, 0, iRead);
 
                                // Convert read bytes to string.
 
                                ByteArrayOutputStream x = new ByteArrayOutputStream();
 
                                x.write(byText);
 
                                st += x.toString();
 
                            } while (iRead > 0);
 
                            // Close input stream, and print webpage.
 
                            is.close();
 
                            System.out.print(st);
 
                        } catch (Exception e) {
                        }
 
                        return null;
                    }
                };
 
        // Set callback functions in HttpRequest object
 
        hr.$onInput.set(fInput);
        hr.$onDoneRead.set(fDoneRead);
 
        // Fetch webpage
 
        hr.enqueue();
    }
}

Now, this much seems to work. I create the variable in the main JavaFX script:

var httpTest = HttpTest { };

…and in my run() function, call:

httpTest.go();

…and it goes. This is as far as I’m going to take it, as I think it’ll make more sense to do the JavaFX stuff in JavaFX and pass the HTML through my parser object as a String, but maybe there’s an interesting use for this somewhere.

Leave a Reply

Your email address will not be published.