You can create Swing applications with embedded JavaFX content. This page describes how to deploy such applications.
This topic contains the following sections:
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:
The same packaging tools can be used to package your Swing applications.
The Deployment Toolkit can be used to embed your Swing application in a web page or launch it from a browser.
Your application can be bundled and packaged as an installable package, using the same technique as for self-contained applications.
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."
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>
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:
Explicitly define a code splash callback onGetSplash
. For example; add the markup in Example 6-2 to Example 6-1:
Example 6-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 19.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 6-3.
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:
Define the JavaFX namespace with jfx:.
Specify that the minimum required version of the JRE is JRE 7 update 6 or later
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"/> </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.
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:
Passing JVM options or arguments to your application dynamically. See Section 19.3.4, "Specify Platform Requirements and Pass JVM Options" and Section 19.3.3, "Pass Parameters to a Web Application."
Usage procedures are almost the same as for JavaFX applications. The only requirement is to specify the Swing toolkit as one of the platform requirements.
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="//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:
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 6-6.
Provide a custom splash callback that does nothing, as shown in Example 6-7 and in Section 19.3.6, "Disable the HTML Splash Screen".
Example 6-7 Custom Callback to Disable the HTML Splash Screen
<html> <head> <SCRIPT src="//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>