Reputation: 120
is it possible to enrich a Swing-based JApplet (running in a web browser) with JavaFX content that works for Java7 clients? I tried the following things, but without success:
a) Swing JApplet: If I try to add a JFXPanel to a JApplet I get java.lang.ClassNotFoundException: javafx.embed.swing.JFXPanel at runtime.
b) Swing JApplet including javafx_version parameter (version 2.2+, see generated APPLET tag when using dtjava.embed() of the deployment toolkit): Adding a JFXPanel to a JApplet results in java.lang.ClassCastException: SwingInterop cannot be cast to javafx.application.Application at runtime.
c) JavaFX Applet (javafx.application.Application): It seems that swing content can only be shown in a new JFrame (as shown in the official Oracle example at http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html) but not in a JavaFX Applet (javafx.application.Application) itself. SwingNode introduced with Java8 would obviously be the way to go but I didn't found a reliable alternative for Java7.
Is there any way to get option a) running by adding the jfxrt.jar to the classpath (as option b) is obviously doing somehow)?
Upvotes: 4
Views: 1223
Reputation: 159576
Oracle have a detailed tutorial on JavaFX in Swing Applications for Java 7.
I suggest you follow that tutorial.
Note that packaging jfxrt.jar with the application is not necessary, and is not recommended (for all the reasons you have in your comment).
Just copy and pasting samples from the Oracle tutorial in case their link goes dead.
Using JavaFX Ant Tasks to Package a Swing Application with Integrated JavaFX Content (key is toolkit="swing"
):
<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>
If you aren't using the JavaFX packaging tools, you can still use JavaFX, just edit your jnlp file, to set up the resources with the jfx namespace:
<?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>
And in your html page, you embed the Java deployment toolkit and make it JavaFX aware:
<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>
I want to show JavaFX content (a JFXPanel) within the area of a JApplet within a browser window.
That is exactly what this answer explains how to do (at least the packaging portion), the JFXPanel javadoc describes the rest.
For an executable example of embedding JavaFX in a Swing Applet, see the JavaFX "SwingInterop" sample code. You can download the SwingInterop sample code from the "JDK 8 Demos and Samples" link off of the Java download page. A Java 7 equivalent would also be available from the Java 7 download archives.
Upvotes: 2