Jim has posted 66 posts at DZone. You can read more from them at their website. View Full User Profile

Using the Java Deployment Toolkit with JavaFX Applets

09.25.2008
| 9597 views |
  • submit to reddit

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.

BindToFunctionApplet_SDK_Preview

Note: Thanks to reader "mbien" for pointing out 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

Published at DZone with permission of its author, Jim Weaver.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jacek Furmankiewicz replied on Thu, 2008/09/25 - 6:39am

I have the latest version of the Update 10 RC and when loading your page it still prompts me for a proxy password.

Is the Java update going to finally properly read the proxy settings from the browser instead of prompting the user on every applet? This makes the user experience very poor compared to Flex and Silverlight, both of which do not exhibit this behaviour.

Jacek Furmankiewicz replied on Thu, 2008/09/25 - 8:47am

BTW, I submitted a P1 defect for this in June:

https://jdk6.dev.java.net/issues/show_bug.cgi?id=21

No response from anybody yet...is this normal?

David Brackeen replied on Thu, 2008/09/25 - 11:44am

Jim - Is it possible to compile for Java 1.5 for Mac users can try it? (Java 6 for Mac exists but doesn't work in 32-bit browsers) 

Jacek - you probably need to submit (or search for) the bug at bugs.sun.com 

Otengi Miloskov replied on Thu, 2008/09/25 - 12:33pm in response to: David Brackeen

JavaFX needs Java6. Java 6 for mac is a 64 bit plugin so as you said it will not work on 32bit browsers.

For fix this the only way is if there is way to compile on mac Safari or Firefox to 64bit or apple should release Java6 for 32bit plugin architecture.

The mac always and always will be a piece of garbage for Java. Poor IPhone folks they still living with the NDA, apple folks are a bunch of Sob's, I think Microsoft is much better and pragmatic than Apple but lets not get into the apple vs pc in here.

Otengi Miloskov replied on Thu, 2008/09/25 - 12:41pm

By the way, anyone really is using JavaFX for real not just playing around?, Because I don't see much momentum with it. I would like to know if there is plans for a designer tool or a plugin for eclipse, more samples and forums and more tools and more books, widespread adoption, etc?. if not better I go back to Flex.

Ben Glancy replied on Fri, 2009/07/10 - 12:32pm

I can't get MY JavaFX applet to work on the Mac but for some reason that same mac will quite happily run the JavaFX applets on the Sun examples webpage. Which is very annoying, because I have followed the sun's deployment advice right down to the last word. It's almost as though they got somethign I don't! Problem I'm surprised it works because it keeps using version 1.5 or so instead of its 64 bit Java SE 6 which I'm told by the sun website is required for JavaFX. So exactly what is going on who knows. I keep getting a NumericLocation Exception on the mac.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.