« Vikings and Wizards in JavaFX | Main | Some Perspective in JavaFX »

September 19, 2008

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.

BindToFunctionApplet_SDK_Preview

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

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54f133d698834010534b0cfad970b

Listed below are links to weblogs that reference Using the Java Deployment Toolkit with JavaFX Applets:

Comments

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

Can i implement this javafx applet using NetBeans 6.0
thanks for the hint.

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).

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 :/

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

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???

>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

"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

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

"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

"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

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... :)

"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

the colors becoming worse after each resurrection ;-)

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))

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

My Photo

Upcoming Speaking Engagements:


  • Stephen Chin and Jim Weaver speaking about JavaFX Platform

  • Speaking on JavaFX and Java at Øredev in Malmö, Sweden on 2-6 November, 2009

Upcoming JavaFX Training:


  • Developing Secure, Rich Internet Applications Hosted on a Variety of Clients Using JavaFX Technology

Enter your email address:

Delivered by FeedBurner

Available now as early access eBook


  • Click book image above to obtain eBook

Twitter Updates

    follow me on Twitter

    Affiliations:

    DZone Links:


    July 2009

    Sun Mon Tue Wed Thu Fri Sat
          1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31  

    Disclaimer:

    • By reading this site, you are agreeing that under no circumstances will Veriana Networks, Inc. or its affiliates be responsible for (1) any information contained on or omitted from the site, (2) any person's reliance on any such information, whether or not the information is correct, current or complete, (3) the consequences of any action you or any other person takes or fails to take, whether or not based on information provided by or as a result of the use of the sites.