The New Draggable Applet Feature in the Java SE 6 Update 10 Plug-In


Java SE 6 Update 10 reintroduces the concept of the applet, which unifies the desktop and browser experience.

Contents

Introduction

Starting in Java SE 6 Update, the new Java Plug-in includes a draggable feature for applets. This feature allows users to bring together their browser and desktop experience.

As a Java technology developer, you can use the rich functionalities in the Java platform to create an applet and allow the user to bring your applet to the desktop. This feature increases flexibility in application deployment and takes advantage of Java Web Start technology to increase the ease of deploying Java Network Launching Protocol (JNLP) applets.

Your comments, suggestions, and other feedback on this capability are welcome. Contact the development team through the Java Plug-in forum.

What You Need

The new Java Plug-in is designed to work in the following browsers:

 

  • Internet Explorer 6 and 7 on Windows XP and Vista
  • Firefox 3 on Windows XP, Windows Vista, Solaris, and Linux

Note that Firefox 2 uses the Classic Java Plug-in and does not provide the new draggable applet feature.

Next, download and install Java SE 6 Update 10 or later. You don't necessarily need to install the JDK. Install only the JRE to experience the new features in Java SE 6 Update 10 if you are not compiling your code.

The New Draggable Applet Feature

The new Java Plug-in allows the user to drag your applet from the browser onto the desktop, with the applet continuing to run. Because the Java Plug-in uses Java Web Start technology, when the user closes the web page, you can prompt the user to decide whether to create a shortcut on the desktop. With the shortcut, the user can now directly launch your applet without opening a web browser and searching for your applet.

An applet that requires LiveConnect will lose its session when the user closes a web browser.

To take advantage of this new draggable feature in your applet, enable the draggable function within your Applet tags:

<APPLET archive="my_applet.jar" code="MyApplet" width="300" height="300">
    <PARAM name="draggable" value="true">
</APPLET>
 

In addition to the new parameter support of this draggable feature, new methods have been implemented in the Applet class. By overriding the following methods, you can configure the behavior of your applets when the drag event happens. The following are the newly implemented methods:

  • public boolean isAppletDragStart(MouseEvent e);
  • public void appletDragStarted();
  • public void appletDragFinished();
  • public void setAppletCloseListener(ActionListener l);
  • public void appletRestored();

If your applet supplies the method public void appletDragStarted();, it will be called when any of these conditions arise:

  • The applet has been removed from its original parent.
  • It has been placed into the new undecorated top-level Frame or JFrame.
  • The new Frame or JFrame is not yet visible.

This gives you the opportunity to change the state of decorations on the frame, use the experimental functionality in Java SE 6 Update 10 to make the frame translucent (useful when building shaped widgets), and perform other operations.

If your applet supplies the method public void appletDragFinished();, it will be called once the applet has been dropped onto the desktop.

By default, a small floating Close button will be drawn above and to the right of the applet's region, and this will track the applet as it moves around the screen. You can customize this user interface.

If your applet defines the method public void setAppletCloseListener(ActionListener l);, then it is assumed that your applet will draw its own Close button inside its region. In this case, you should save the ActionListener object passed in to this method call when it is made by the Java Plug-in, and call its actionPerformed method to initiate the shutdown of the applet in response to the user's input. The ActionEvent argument to the actionPerformed method may be null. It is ignored.

If the user clicks the Close button while the web page that originally contained the applet is still visible, then the applet will be placed back into its original location on the page, and the method public void appletRestored(); will be called. If the applet has been disconnected from the browser, as this article will soon demonstrate, then normal applet shutdown will be initiated: The stop and destroy methods will be called.

If the user drags an applet out of the web page, then navigates away from that web page or shuts down the web browser, the applet is conceptually disconnected from the web browser. This means that certain services, such as Java and JavaScript communication, are disabled.

Other browser services, such as the implementation of AppletContext.showDocument(), are replaced with alternate implementations that have slightly less fine-grained control, specifically the following:

  • Any attempt to call from Java to JavaScript through the netscape.javascript.JSObject class will result in a JSException.

  • Any calls to AppletContext.showDocument will open a new browser window. If a call is made to showDocument(URL url, String target), the target will be ignored.

  • Calls to AppletContext.showStatus will display the string in the Java Console, if it is open, rather than in the browser's status bar.

  • Queries of proxy settings and cookies, which are normally redirected back to the web browser, are instead handled by the platform-specific settings. For example, on a Microsoft Windows platform, the proxy settings for Internet Explorer are implicitly the system wide proxy settings, and these will be used for a disconnected applet even if Mozilla Firefox was originally used to launch the applet.

Two Demo Applets

Press the Alt key and the left mouse button to drag the applet off the browser and onto the desktop. Try this with the following clock-sample applet. You must have Java SE 6 Update 10 or later, and either Mozilla Firefox 3 or Internet Explorer 6 or 7 in order to use this feature.

For source code, refer to the clock sample delivered with the Java SDK.

The following JNLP applet uses a JNLP file to specify the application attributes and shortcuts that will be created on the user's desktop. When the user presses the Alt key and the left mouse button to drag the applet out of the browser, then closes the browser, a pop-up dialog box asks the user to decide whether to create a shortcut on the desktop. This feature greatly enhances the flexibility in deploying your applets to the end-user's desktop.

Here are the JNLP file and the source code for this applet.

About the Java SE 6 Update 10 Feed Reader

The Java SE 6 Update 10 Feed Reader demo is a simple model-view-controller (MVC) design application that uses some of the latest features provided in Java SE 6 Update 10, including the JNLP applet, the draggable applet, and the new Java system properties for Pack200. To read more about the new features, see the For More Information section at the end of this article.

The feed-reader demo retrieves feeds from several Sun blogs every 10 minutes and provides you with the latest Java SE 6 Update 10 early access news. It provides entries from SDN Program News, the Aquarium (GlassFish), and Oracle employee blogs.

In addition to the new features in Java SE 6 Update 10, this demo uses the Desktop API, which is new in Java SE 6. Make sure that you have installed the latest Java SE 6 build.

What's Next?

Java SE 6 Update 10 is an update release with new features that target the consumer end-user experience. These new features include Java Kernel, Java Quick Starter, Java Deployment Toolkit, Next Generation Java Plug-in, Nimbus Look and Feel, and new 3D acceleration support. For a list of the new features, see the Java SE 6 Update 10 Release Notes.

To try these new features, download the latest Java SE 6 Update 10.

For a high-level overview of the new features, see Ethan Nicholas's article, Introducing Java SE 6 Update 10.

For detailed information about the new Java Plug-in and this feature, see the release notes.

For More Information

Java SE 6 Update 10 Release Notes
Next-Generation Plug-in Release Notes
New Java System Properties

About the Author

Roger Yeung is a software engineer in the Java SE platform at Sun Microsystems. His interests include security as well as Java Web Start, Sun SPOT, and GIS technologies.