Deploying JavaFX Applications

Previous
Next

10 JavaFX in Swing Applications

You can create Swing applications with embedded JavaFX content. This page describes how to deploy such applications.

This page contains the following topics:

See also Section 2.7, "Deploying Swing and SWT Applications with Embedded JavaFX Content".

10.1 Overview

Developers working on existing Swing applications may still take advantage of new JavaFX features by integrating JavaFX content into their Swing applications. (See the tutorial JavaFX and Swing Applications.)

Deployment of a Swing application with embedded JavaFX content on the web is similar to deployment of a regular Swing application as a Web Start (JNLP) application or applet. See the Java Tutorials lessons on Java applets and Web Start applications.

However, to be able to use JavaFX, the deployment descriptor of your application (JNLP file) needs to express a dependency on JavaFX Runtime.

JavaFX Ant tasks are recommended for packaging hybrid applications, as described in Section 10.2, "Packaging with JavaFX Ant Tasks."). Alternatively, you can tweak your existing packaging process manually, as described in Section 10.3, "Packaging without JavaFX Tools."

10.2 Packaging with JavaFX Ant Tasks

As of JavaFX 2.2, you can use same set of Ant tasks (see Chapter 5, "Packaging Basics") to package Swing applications with integrated JavaFX content. You only need to mark that the application's primary UI toolkit is Swing, using the toolkit="swing" attribute of <fx:application>.

The resulting package will be very similar to the package for pure JavaFX applications. (See Section 5.2, "Base Application Package.") The only difference is that there will be two deployment descriptors - one for deploying as a Swing applet and another for launching your application using Web Start.

Example 10-1 shows a sample Ant task for a Swing-JavaFX application that uses the toolkit="swing" attribute.

Example 10-1 Using JavaFX Ant Tasks to Package a Swing Application with Integrated JavaFX Content

<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      
        uri="javafx:com.sun.javafx.tools.ant"
        classpath="${javafx.sdk.path}/lib/ant-javafx.jar"/>
 
<fx:jar destfile="dist-web/ColorfulCircles.jar">
    <fx:application refid="myapp"/>
    <fileset dir="build/classes/">
        <include name="**"/>
    </fileset>
</fx:jar>
 
<fx:deploy width="800" height="600" outdir="dist-web" 
        outfile="SwingInterop">
    <fx:info title="Swing Interop"/>
    <!-- Mark application as a Swing app -->
    <fx:application id="myapp" 
            mainClass="swinginterop.SwingInterop"
            toolkit="swing"/>
    <fx:resources>
        <fx:fileset dir="dist-web" includes="SwingInterop.jar"/>
    </fx:resources>
</fx:deploy> 

10.2.1 Enabling an HTML Splash Screen

Swing applications do not have a default preloader, so there is no code to hide the HTML splash screen when the application is ready. Therefore, by default, the generated HTML page will not use an HTML splash screen.

To enable a splash screen, do both of the following:

  • Explicitly define a code splash callback onGetSplash. For example; add the markup in Example 10-2 to Example 10-1:

    Example 10-2 Defining a Code Splash Callback onGetSplash

    <fx:callbacks>
        <!-- force use of default splash handler -->
        <fx:callback name="onGetSplash">
                (new dtjava.Callbacks()).onGetSplash
        </fx:callback>
    </fx:callbacks>
    

    See Section 7.2, "Callbacks" for more information about callbacks and <fx:callbacks> for further details on the use of <fx:callbacks>.

  • Add code to explicitly hide the HTML splash screen when your application is ready, by using LiveConnect to call the JavaScript function dtjava.hideSplash() from Java code, as shown in Example 10-3.

Example 10-3 Hide the HTML Splash Screen When the Application is Ready

Applet myApplet = ...;
//appId is id used in the dtjava.embed() call String appId = "sampleApp";
 
JSObject window = JSObject.getWindow(myApplet);
try {
        window.eval("dtjava.hideSplash('"+appId+"');");
    } catch(Throwable t) {
    ...
}

10.3 Packaging without JavaFX Tools

If your project already has existing support for packaging as a Web Start application or applet, then it might be easier to tweak the template of the JNLP file directly.

To express the dependency on JavaFX Runtime, you need to edit the JNLP to do the following:

  • Define the JavaFX namespace with jfx:.

  • Add the <jfx:javafx-runtime> tag and specify the minimum required version of JavaFX.

  • Specify that the minimum required version of Java Runtime is Java Runtime 7 update 6 or later

Example 10-4 shows an example of these modifications to the deployment descriptor.

Example 10-4 Modify the Deployment Descriptor

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0"
        xmlns:jfx="http://javafx.com"
        href="SwingAppWithJavaFXContent.jnlp"
        ...>
     ....
    <resources>
        <j2se version="1.7.0_06+" 
            href="http://java.sun.com/products/autodl/j2se"/>
        <jfx:javafx-runtime version="2.1+" 
            href="http://javadl.sun.com/webapps/download/GetFile/
                    javafx-latest/windows-i586/javafx2.jnlp"/>
    </resources>
    ...
</jnlp> 

No changes are required on the HTML side. However, you may also use the JavaFX Deployment Toolkit to integrate your content with the web page, as shown in the following examples.

10.3.1 Using the Deployment Toolkit

It is recommended that you use the Deployment Toolkit to deploy your application as an applet or launch it from a web page. The Deployment Toolkit greatly simplifies deployment routines, such as ensuring that JavaFX Runtime is available. The Deployment Toolkit also has a number of bonus features, such as:

For example Example 10-5 shows an example of web page code for launching a Swing Web Start application. This code is the same as that used to launch JavaFX applications.

Example 10-5 Scripts and Markup in HTML Page for a Web Start Application

<html>
    <head>
            <SCRIPT src="http://java.com/js/dtjava.js"></SCRIPT>
            <script>
                function launchApplication(jnlpfile) {
                dtjava.launch(
                    { url : jnlpfile },
                    {
                        javafx : '2.2+',
                        toolkit: 'swing'
                    },
                    {}
                );
                return false;
            }
        </script>
    </head>
    <body>
        <h2>Test page</h2>
        <a href='SampleApp.jnlp' 
        onclick="return launchApplication('SampleApp.jnlp');">Click</a> 
        to launch test app.
    </body>
</html>

Example 10-5 is simplistic, but you can use other features of the dtjava.launch() method in the Deployment Toolkit for Swing applications, as needed. For example, you can embed the JNLP file into the web page for faster startup.

For applets, the approach is similar. Applets can be embedded into a web page using the dtjava.embed() function. You must also specify the 'swing' toolkit to indicate a preference of application type for the Deployment Toolkit.

Another caveat concerns built-in HTML splash support in the Deployment Toolkit. Swing applications do not have a default preloader, so there is no code to hide the HTML splash screen when the application is ready. To address this difference, do either of the following:

  • Add code to explicitly hide the HTML splash screen when your application is ready, by using LiveConnect to call the JavaScript function dtjava.hideSplash() from Java code, as shown in Example 10-6.

    Example 10-6 Hide the HTML Splash Screen When the Application is Ready

    Applet myApplet = ...;
    //appId is id used in the dtjava.embed() call String appId = "sampleApp";
     
    JSObject window = JSObject.getWindow(myApplet);
    try {
            window.eval("dtjava.hideSplash('"+appId+"');");
        } catch(Throwable t) {
        ...
    }
    
  • Provide a custom splash callback that does nothing, as shown in Example 10-7 and in Section 7.3.6, "Disable the HTML Splash Screen".

Example 10-7 Custom Callback to Disable the HTML Splash Screen

<html>
    <head>
        <SCRIPT src="http://java.com/js/dtjava.js"></SCRIPT>
        <script>
            function embedApp() {
                dtjava.embed(
                    {
                                 id : 'sampleApp',
                                url : 'SampleApp_browser.jnlp',
                        placeholder : 'app-placeholder',
                              width : 960,
                             height : 720
                    },
                    {
                        javafx : '2.2+',
                        toolkit: 'swing'
                    },
                    {   onGetSplash: function() {} } //disable splash
                );
            }
            <!-- Embed Swing application into web page after page is loaded -->
            dtjava.addOnloadCallback(embedApp);
        </script>
    </head>
    <body>
        <h2>Test page</h2>
        <!-- Applet will be inserted here -->
        <div id='javafx-app-placeholder'></div>
    </body>
</html>
Previous
Next