Using the jnlpapplet framework
Download the jnlpapplet package as a
zip archive and extract the contents
of the file. On Unix like systems (e.g. Linux or Mac OS X) you
can extract the contents of the tar archive as follows:
tar -zxvf jnlpapplet-1.0.tar.gzPlace the following files from the package in a publicly acesssible location on your web server:
- jnlpapplet.css: This file contains CSS elements that can be used to render the applet window and launch buttons. You can modify the css to suit your website's design.
- jnlpapplet.jar: This Java archive file contains the java framework classes needed to launch applets and applications seamlessly. The code here is described in the previous article
- jnlpapplet.jnlp: The JNLP (Java Network Launch Protocol) file that specifies how an applet is launched. You can modify this to suit the needs of your website.
- jnlpapplication.jnlp: The JNLP (Java Network Launch Protocol) file that specifies how a Java Web Start application is launched. You need to modify this to launch the application from your server and to suit the needs of your website.
stylesheet to the HTML header of your web pages. You
can do this by adding the following lines to the HTML header
section of your web pages.
Specify the applet parameters in the page. The
needs to be added to the HTML header of the web page below the
script references described above.
jnlpapplication_url: This variable specifies the URL of the jnlpapplication.jnlp file on your web server. The same URL must be embedded in codebase section of the jnlpapplication.jnlp file. (See below.)
attributes: This is an array of attributes passed to the Java browser plugin that starts the applet. The code attribute and the archive attribute are required. They specify the class to be run and the jar file in which it exists, respectively. The height and width attribute specify how tall and and wide the applet window should be.
paremeters: This is an array of values that the Java plugin passes to the applet. The demo application takes 3 parameters. The jnlp_href parameter is required and is used by the
javax.swing.JAppletframework class to retrieve further information from the JNLP file. The fontSize and backround parameters are used by this demo applet to set the font size of the text in the applet window and the background respectively. Your code may use other parameters that may be interpreted differently.
version: This is set to the minimum acceptable version of the Java runtime. If the version of the runtime installed on the readers machine is lower than this, the reader will be redirected to the Java download page and prompted to install or upgrade to the latest version.
appletCreated: This variable indicates which applets on the page have been created. It should always be initialized to an empty array.
Add HTML and CSS elements for the applet and the
launch button. The HTML snippet below will result in
an unlaunched applet window, with some initial text.
applet0)identifies the applet on the page. If you have more than one applet on the page use a different id for each applet. The class
applet-windowis defined in jnlpapplet.css and renders an empty box around the area where the applet will be placed. The width and height styles should be specified to match that specified in the javacript attributes passed to the java plugin to ensure that the window dimensions don't change when the applet is started by the user. When the user clicks the launch button, the applet is launched with a call to
createAppletthat will, if necessary, install the Java runtime on the user's machine.
To launch a Java Web Start application instead, use the following HTML snippet:
How the framework works
The key difference between the delayed launch framework provided
provides methods that work after the page has been rendered. The
createApplet() method takes the id of the HTML div
be replaced. It generates the HTML applet tag appropriate for the
browser and replaces the specified div element with the applet
tag. The key bit of code in the script accomplishes this is shown
var appletTag = startApplet + '\n' + params + '\n' + endApplet; parentTag.innerHTML = appletTag;
The next article in this series will examine how the CSS launch buttons are created.