Sunday, May 11, 2008

Dragging Java applets out of the browser

As we saw from the impressive JavaOne demonstrations, Sun are aiming to turn JavaFX applets into desktop widgets by allowing the user to drag them out of the browser to their desktop.

As we also saw from the number of people visiting our booth, everyone seemed to think this was a JavaFX feature. Well, it's not, and I've just got one of my JavaOne session example applets doing the same thing that was demonstrated at JavaOne, although I will admit the JavaFX demos look a damn site better than my little demo applet.

The key thing you need to accomplish dragging an applet out of a browser is the Java 6 Update 10 installed on your machine and running as the browser plugin. This means you need Firefox Beta 3 if you don't use IE. You can download the beta from http://java.sun.com/javase/downloads/index.jsp

Once you have this, then enabling applet dragging requires two things:

1 - Deploy your applet using a JNLP file (previously just used for WebStart)
2 - Add the 'draggable' parameter to your applet.

Step 1:
You can find more details on JNLP support in the new Update 10 plugin at http://java.sun.com/javase/downloads/ea/6u10/newJavaSystemProperties.jsp, but all that a JNLP file does is provide a standard mechanism for describing an applet, its dependent JAR files, its JVM parameters and JRE version requirements.

Here is the one that I am using:


Most of this is not very interesting, and is just describing how to run the applet. What we need next is to use this JNLP file to deploy our applet, which we need to change our applet's HTML to do.

Step 2:
To deploy your applet with a JNLP file then you need to change your APPLET tag in the HTML page to reference the JNLP file, like so:

The main things to note here are the jnlp_href value, which points at our JNLP descriptor file, and the draggable parameter, which enables our new dragging functionality.

Once you have these in place, then run your applet, and when it is running try to drag it out of the browser using ALT + Left Click. If all goes well, your applet will be living large outside of the browser.

The cool thing is that the applet is not destroyed and recreated during this process - my applet can be dragged mid-connection loop to the server and the connections remain uninterrupted during the drag process, and continue once the applet is outside of the browser, as you can see below:



I'll try some more interesting examples soon, and the next step is to deploy AltioLive using a JNLP file and run it outside the browser, which will be very interesting.

2 comments:

Unknown said...

Any live examples?

Jim said...

Hi,

If you download the example code I used for my JavaOne session I have added JNLP support to one of the examples.

See here:

http://www.altio.com/DeveloperCentre/Developers/JavaOne2008-Session-Examples.aspx

and look for Example 4 details.