Using the Java Deployment Toolkit with JavaFX Applets
First, let me apologize for resurrecting the very humble JavaFX program shown below, but I want to keep this example very succinct. This will enable you to use it as "starter code" for JavaFX applet deployment. Note: To see more functional JavaFX programs, please see articles in the JFX Custom Nodes category.
Note: Thanks to reader "mbien" (see comments) for pointing our that the colors of the original applet in this post were hideous (my words). I then consulted graphics designer Mark Dingman of Malden Labs who gave me a graphical mock-up from which I created the above applet. Here's the code for this applet, updated for the JavaFX SDK preview:
/*
* BindToFunctionApplet.fx - A compiled JavaFX program that demonstrates
* how to create JavaFX applets.
* It also demonstrates binding to a function.
*
* Developed 2008 by Jim Weaver (development) and Mark Dingman (graphic design)
* to serve as a JavaFX Script example.
*/
package com.javafxpert.bind_to_function;
import javafx.application.*;
import javafx.ext.swing.*;
import javafx.scene.*;
import javafx.scene.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.scene.transform.*;
import java.lang.Math;
class CircleModel {
attribute diameter:Integer;
bound function getArea():Number {
Math.PI * Math.pow(diameter / 2, 2);
}
}
Application {
var cModel = CircleModel {};
var componentViewRef:ComponentView;
var stageRef:Stage;
stage:
stageRef = Stage {
var labelFont = Font {
name: "Sans Serif"
style: FontStyle.PLAIN
size: 32
}
fill:
LinearGradient {
startX: 0.0
startY: 0.0
endX: 0.0
endY: 1.0
stops: [
Stop {
offset: 0.0
color: Color.rgb(0, 168, 255)
},
Stop {
offset: 1.0
color: Color.rgb(0, 65, 103)
}
]
}
content: [
Circle {
centerX: 250
centerY: 250
radius: bind cModel.diameter / 2
fill:
LinearGradient {
startX: 0.0
startY: 0.0
endX: 0.0
endY: 1.0
stops: [
Stop {
offset: 0.0
color: Color.rgb(74, 74, 74)
},
Stop {
offset: 1.0
color: Color.rgb(9, 9, 9)
}
]
}
},
Text {
font: labelFont
x: 30
y: 70
fill: Color.BLACK
content: bind "Diameter: {cModel.diameter}"
},
Text {
font: labelFont
x: 260
y: 70
fill: Color.BLACK
content: bind "Area: {%3.2f cModel.getArea()}"
},
componentViewRef = ComponentView {
transform: bind
Translate.translate(40, stageRef.height - 30 -
componentViewRef.getHeight())
component:
Slider {
minimum: 0
maximum: 400
preferredSize: bind [stageRef.width - 80, 20]
value: bind cModel.diameter with inverse
}
}
]
}
}
Why Use the Java Deployment Toolkit for Java Applets?
According to Sun's Java Deployment Toolkit overview page, "Desktop clients have a wide variety of Java Platforms installed, from the Microsft VM to Sun's latest Java SE 6 updates. They run various operating systems from Sun, Microsoft, Apple, Red Hat, and others, and are connected to the internet at a wide range of connection speeds. How are content providers to deliver Java content to all of these clients with the best possible user experience?
Various sources have published JavaScript techniques for detecting and deploying the Java Platform for use by Java Plug-In applets and Java Web Start applications. These scripts generally have serious limitations and fail to support the varied combinations of browser, OS, and configuration options found on today's clients.
The Java Deployment Toolkit allows developers to easily deploy applets and applications to a large variety of clients with JavaScripts. It also provides advice on using some of the most powerful features available in Java Web Start and Java Plug-In, and an outline of the differences between these two deployment vehicles."
In a nutshell, the Java Deployment Toolkit is a JavaScript library maintained by Sun and always available at runtime by your HTML code. This library has several methods that perform tasks such as sensing Java-related infrastructure and installing the JRE on client machines. We'll use one of these methods, namely runApplet, to run a JavaFX applet with a specified minimum JRE version. Here's the HTML and JavaScript code I'm using to deploy today's example applet:
<html>
<script src="http://java.com/js/deployJava.js"></script>
<script>
var attributes = {codebase:'http://jmentor.com/JFX/BindToFunctionApplet',
code:'javafx.application.Applet.class',
archive:'BindToFunctionApplet.jar, javafxrt.jar, Scenario.jar, javafxgui.jar, javafx-swing.jar',
width:500, height:500, java_arguments:'-Djnlp.packEnabled=true'};
var parameters = {"ApplicationClass":"com.javafxpert.bind_to_function.BindToFunctionApplet",
"draggable":"true"};
var version = '1.6.0' ;
deployJava.runApplet(attributes, parameters, version);
</script>
</html>
Notice that the above code enables dragging the applet onto the desktop, as well as using Pack200 formatted JAR files, if the client machine has Java SE 6 update 10 installed. Give the applet a whirl to see its deployment behavior on your machine. By the way, according to the Java SE 6 Update 10 plug-in docs, "by default, the gesture to drag the applet out of the web browser is Alt + Left click + Drag."
Thanks,
Jim Weaver
JavaFXpert.com weblog






NOT WORKING IN FIREFOX WITH JRE 1.6.0_11(Windows Vista)
Here is the error i got in the console
Java Plug-in 1.6.0_11
Using JRE version 1.6.0_11 Java HotSpot(TM) Client VM
User home directory = C:\Users\Lenin
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to
----------------------------------------------------
load: class javafx.application.Applet.class not found.
java.lang.ClassNotFoundException: javafx.application.Applet.class
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: javafx.application.Applet.class
Posted by: Lenin | December 19, 2008 at 12:25 AM
Can i implement this javafx applet using NetBeans 6.0
thanks for the hint.
Posted by: wahyu | November 17, 2008 at 09:59 PM
The deployment process does not work in Opera (9.6 preview build). I have Java 1.5.0_04 installed on Windows XP, and it happily starts the applet, only to recognize that it does not know the class format (which is compiled for 1.6, i guess).
Posted by: Eabin | September 25, 2008 at 12:23 PM
Jim,
Still not starting. Not even getting anything in the console. Is there any way I can trace what's (not) happening?
Beginning to feel it's this client not chrome specific :/
Posted by: Erik | September 23, 2008 at 04:06 AM
Remon,
unfortunately, the Java applet gesture conflicts with the Gnome action for dragging a window. Sun really should make this gesture platform-specific. In the meantime, remap the Gnome gesture as described here: http://weblogs.java.net/blog/cayhorstmann/archive/2008/09/applet_dragging.html
Posted by: Cay Horstmann | September 22, 2008 at 10:40 AM
The app works as it should on Ubuntu/Firefox with Java 6 Update 10 installed. The only thing that bothered me was that I had to agree to a security dialog. Is this really necessary???
Posted by: Cay Horstmann | September 22, 2008 at 10:37 AM
>Good point. I haven't been consulting a graphic designer on this example, but I will do that ASAP and change the colors :-)
wow, that looks much better! thanks
Posted by: mbien | September 22, 2008 at 05:45 AM
"I can't get dragging the applet to the desktop to work.
Ubuntu 8.04, Java 1.6.0_10-rc2-b31, FireFox 3.0.1"
Remon,
Are you holding the ALT key down while dragging? I should have put this in the article (I'm adding it now), but according to the Sun docs on the following page:
http://java.sun.com/javase/downloads/ea/6u10/plugin2/index.jsp#EXPERIMENTAL_FUNCTIONALITY
"By default, the gesture to drag the applet out of the web browser is Alt + Left click + Drag"
Thanks,
Jim Weaver
Posted by: Jim Weaver | September 19, 2008 at 04:22 PM
I can't get dragging the applet to the desktop to work.
Ubuntu 8.04, Java 1.6.0_10-rc2-b31, FireFox 3.0.1
Posted by: Remon Sinnema | September 19, 2008 at 03:59 PM
"on mac os x the behaviour is more than confusing..."
Hansi,
Thanks for reporting the experience you had on the Mac. I've submitted it to the Java Deployment Toolkit forum in the following post:
http://forums.java.net/jive/thread.jspa?threadID=47389
I'll be watching for a response, and please feel free to as well.
Thanks again,
Jim Weaver
Posted by: Jim Weaver | September 19, 2008 at 03:20 PM
"I'm not geting the example to run under chrome, however works superb in FF and IE. In Chrome I just get the splash java anim , jvm loaded then nothing."
Eric,
Can you please try again and post another comment? There was a point in time after I posted this article that I removed one of the JAR files to test whether Chrome would use the Pack200 versions. It exhibited the same (lack of) symptoms that you described.
Thanks,
Jim Weaver
Posted by: Jim Weaver | September 19, 2008 at 03:01 PM
on mac os x the behaviour is more than confusing.
i get redirected to http://java.com/de/download/apple_manual.jsp?host=java.com&returnPage=http://jmentor.com/JFX/BindToFunctionApplet/BindToFunctionPage.html&locale=de
why is this confusing?
#1 i wasn't notified about the redirect.
#2 i DO have java6 installed. it's just not my default jre (i made 1.5 my default choice because 1.6 broke some apps)
#3 the page i got redirected to isn't helpful at all. it says i can install java6 via software update. hm... it's already there!
for now this is a real showstopper, but i'm pretty sure it'll be sorted out soon... :)
Posted by: hansi | September 19, 2008 at 01:13 PM
"the colors becoming worse after each resurrection ;-)"
mbien,
Good point. I haven't been consulting a graphic designer on this example, but I will do that ASAP and change the colors :-)
Thanks,
Jim Weaver
Posted by: Jim Weaver | September 19, 2008 at 12:15 PM
the colors becoming worse after each resurrection ;-)
Posted by: mbien | September 19, 2008 at 11:33 AM
I'm not geting the example to run under chrome, however works superb in FF and IE.
In Chrome I just get the splash java anim , jvm loaded then nothing.
(1.6.0 u10 rc 2 (b32))
Posted by: Erik | September 19, 2008 at 03:36 AM