Over a million developers have joined DZone.

JavaFX 2.0 in NetBeans RCP

· Java Zone

Learn more about how the Java language, tools and frameworks have been the foundation of countless enterprise systems, brought to you in partnership with Salesforce.

Below follows a prototype of the current state of JavaFX integration into NetBeans RCP. It is a prototype only and does not reflect in any way the final state of this integration. (Hopefully, in fact, it will all end up being a lot simpler and more intuitive than what is described below.)

One of the samples that comes with the JavaFX 2.0 distro is this brick game:

Fortunately, it comes as a NetBeans project:

As you can see, above, the application's Main class extends the "javafx.application.Application" class, which starts the JavaFX lifecycle, together with its own special JavaFX thread.

Now, let's move the brick game to NetBeans RCP. One of the many apps on NetBeans RCP is NetBeans IDE. So, as an example, we're going to learn how to play the JavaFX brick game within NetBeans IDE:

Take the following steps:

  1. Read background documents. Read this NetBeans FAQ by Tom Wheeler and this blog entry by Josch Rittner.

  2. Become a JavaFX partner. So that you can get hold of early JavaFX distros.

  3. Create a JavaFX main class for your NetBeans RCP project. Start by creating a Java library project in NetBeans IDE, add the "jfxrt.jar" from the JafaFX distro as a library, and create this class, which you understand because you've done step 1 of these instructions:
    package mymain;

    import java.lang.reflect.Method;
    import javafx.application.Application;
    import javafx.stage.Stage;

    public class CustomStartup extends Application {

    private static final String NB_MAIN_CLASS = "org.netbeans.core.startup.Main";

    public static void main(String[] args) throws Exception {
    // do whatever you need here (e.g. show a custom login form)
    System.out.println("Launch Java FX");
    long ms = System.currentTimeMillis();
    // create JavaFX scene
    Application.launch(CustomStartup.class, args); // This is the main start up for JavaFX 2.0
    System.out.println("Launched Java FX in " + (System.currentTimeMillis() - ms) + "ms");
    // once you're done with that, hand control back to NetBeans
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    Class mainClass = Class.forName(NB_MAIN_CLASS, true, classloader);
    Object mainObject = mainClass.newInstance();
    Method mainMethod = mainClass.getDeclaredMethod("main", new Class[]{String[].class});
    mainMethod.invoke(mainObject, (Object) args);
    }

    @Override
    public void start(Stage stage) {
    // Nothing to do, forget the stage....
    }

    }

    This is what the Java library project should now look like:

  4. Put the JAR in the boot classpath. Take the JAR that you create from the above Java library project and put it in your application's "platform/core" folder, with the "jfxrt.jar" in the "platform/lib" folder, and all the DLL's in a new "bin" folder:


  5. Port the brick game to a NetBeans module. Create a new module, wrap the "jfxrt.jar" into the module, copy all the classes from the brick game into the module, create a new window, judiciously copy the code from the "Main.java" class into the new window, making use of a JFXPanel, as follows:
    Platform.runLater(new Runnable() {
    @Override
    public void run() {
    Config.initialize();
    Group root = new Group();
    mainFrame = new MainFrame(root);
    Scene scene = new Scene(root);
    scene.setFill(Color.BLACK);
    jFXPanel1.setScene(scene);
    mainFrame.changeState(MainFrame.SPLASH);
    }
    });

    The good news is, once you have a JavaFX scene, you can use "JFXPanel.setScene" and add the JFXPanel to the TopComponent. Then you're done, you've integrated your Scene into Swing, i.e., JavaFX is now running in Swing.

  6. Specify your JavaFX main class. Open the "project.properties" of the module and add this line:
    run.args.extra=-J-Dnetbeans.mainclass=mymain.CustomStartup

    Now run the module. The application (in this case, NetBeans IDE) starts up, the "MyMain.jar" is in the boot classpath, the main class is specified in the module, so JavaFX is started up, then the window opens, and your game begins.

The only real question to ask is "Why". In corporate applications (i.e., the kinds of application requirements that result in NetBeans RCP being used), games are not very useful. Charts, on the other hand, are very usseful indeed and JavaFX comes with its own set of charts, which we'll look at another time.

One very cool thing would be if we had a "JavaFX Starter" module in the NetBeans Plugin Portal. When you'd install this module into any NetBeans RCP application, the "MyMain.jar" above would be put into "platform/core", the "jfxrt.jar" would be put into "platform/lib" and the DLL's would be put into "platform/bin". I.e., all these files would exist in the module and the module would put them into all the right places. The module would then cause a restart of the application, at which point the module's own "run.args.extra" would be used to use the new JavaFX main class shown above. Then anyone could install JFXPanels into their NetBeans RCP applications, without needing to think about the JavaFX startup requirements.

 

Discover how the Force.com Web Services Connector (WSC) is a code-generation tool and runtime library for use with Force.com Web services, brought to you in partnership with Salesforce.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}