Documentation Contents
Java Platform, Standard Edition Deployment Guide
Contents    Previous    Next

6 JavaFX in Swing Applications

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

This topic contains the following sections:

6.1 Overview

Developers working on existing Swing applications can take advantage of JavaFX features by integrating JavaFX content into their Swing applications. See the tutorial JavaFX and Swing Applications for more information.

Not all of the techniques discussed in this guide are applicable to Swing applications with JavaFX content, however, the following techniques are relevant:

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

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

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

6.2 Packaging with JavaFX Ant Tasks

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 is similar to the package for pure JavaFX applications. See Section 5.2, "Base Application Package." The only difference is that there are two deployment descriptors - one for deploying as a Swing applet, and another for launching your application using Java Web Start.

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

Example 6-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> 

6.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 does not use an HTML splash screen.

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

Example 6-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) {
    ...
}

6.3 Packaging without the Packaging Tools

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

To express the dependency on the JavaFX Runtime, make the following changes in the JNLP file:

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

Example 6-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 can also use the Deployment Toolkit to integrate your content with the web page, as shown in the examples in the following sections.

6.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 browser. The Deployment Toolkit simplifies deployment routines, such as ensuring that required JRE is available. The Deployment Toolkit also has several bonus features, such as:

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

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

<html>
    <head>
            <SCRIPT src="http://java.com/js/dtjava.js"></SCRIPT>
            <script>
                function launchApplication(jnlpfile) {
                dtjava.launch(
                    {     url : jnlpfile,
                      toolkit : 'swing'
                    }
                    {
                        javafx : '2.2+'
                    },
                    {}
                );
                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 6-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, use either of the following methods:

Example 6-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,
                            toolkit : 'swing'
                    },
                    {
                        javafx : '2.2+'
                    },
                    {   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>
Contents    Previous    Next

Oracle and/or its affiliates Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.
Contact Us