A Java applet that is deployed by specifying the draggable
parameter can be dragged outside of the browser and dynamically transformed into a Java Web Start application. The Java applet can be dragged by pressing the Alt key and the left mouse button and dragging the mouse. When the drag operation begins, the applet is removed from its parent container (Applet
or JApplet
) and placed in a new undecorated top-level window (Frame
or JFrame
). A small floating Close button is displayed next to the dragged applet. When the floating Close button is clicked, the applet is placed back in the browser. Java applets that can be dragged out of the browser shall henceforth be referred to as draggable applets.
You can customize the behavior of a draggable applet in the following ways:
The following sections describe how to implement and customize a draggable applet. The
MenuChooserApplet
class is used to demonstrate the development and deployment of draggable applets. Open
in a browser to view the Menu Chooser applet on a new page.AppletPage.html
If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 6 update 10 release.
If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.
You can enable the capability to drag an applet by setting the draggable
parameter to true
when deploying the applet, as shown in the following code snippet:
<script src="http://www.java.com/js/deployJava.js"></script> <script> var attributes = { code:'MenuChooserApplet', width:900, height:300 }; var parameters = {jnlp_href: 'draggableapplet.jnlp', draggable: 'true'} ; deployJava.runApplet(attributes, parameters, '1.6'); </script>
You can change the keystroke and mouse button sequence that is used to drag an applet by implementing the isAppletDragStart
method. In the following code snippet, the applet can be dragged by pressing the left mouse button and dragging the mouse:
public boolean isAppletDragStart(MouseEvent e) { if(e.getID() == MouseEvent.MOUSE_DRAGGED) { return true; } else { return false; } }
If the user closes the browser window or navigates away from the page after dragging an applet outside of the page, the applet is said to be disconnected from the browser. You can create a desktop shortcut for the applet when the applet is disconnected from the browser. The desktop shortcut can be used to launch the application outside of the browser. To enable the creation of a desktop shortcut, add the offline-allowed
and shortcut
tags to the applet's Java Network Launch Protocol (JNLP) file.
<information> ... <offline-allowed /> <shortcut online="false"> <desktop /> </shortcut> </information>
Depending on the setting for Shortcut Creation in the user's Java Control Panel, the user might be prompted for confirmation before the shortcut is created.
You can define how your applet can be closed. For example, your Swing applet could have a JButton
to close the applet instead of relying on the default floating Close button.
The Java Plug-in software gives the applet an instance of the ActionListener
class. This instance of the ActionListener
class, also referred to as the close listener, can be used to modify the default close behavior of the applet.
To define how the applet should be closed, implement the setAppletCloseListener
and appletRestored
methods in your applet.
In the following code snippet, the
MenuChooserApplet
class receives the close listener and passes it on to the instance of the
MenuItemChooser
class:
MenuItemChooser display = null; ... display = new MenuItemChooser(); ... public void setAppletCloseListener(ActionListener cl) { display.setCloseListener(cl); } public void appletRestored() { display.setCloseListener(null); }
The MenuItemChooser
class is responsible for controlling the applet's user interface. The MenuItemChooser
class defines a JButton
labeled "Close." The following code is executed when the user clicks this Close button:
private void close() { // invoke actionPerformed of closeListener received from // the Java Plug-in software. if (closeListener != null) { closeListener.actionPerformed(null); } }
Beginning in the Java SE 7 release, when deploying an applet, you can specify that the window of dragged applet should be decorated with the default or customized window title.
To enable window decoration of a dragged applet, specify the java_decorated_frame
parameter with a value of "true"
. To enable a customized window title, specify the java_applet_title
parameter also. The value of this parameter should be the text of the window title.
<script src="http://www.java.com/js/deployJava.js"></script> <script> var attributes = { code:'SomeDraggableApplet', width:100, height:100 }; var parameters = {jnlp_href: 'somedraggableapplet.jnlp', java_decorated_frame: 'true', java_applet_title: 'A Custom Title' } ; deployJava.runApplet(attributes, parameters, '1.7'); </script>
The java_decorated_frame
and java_applet_title
parameters can also be specified in the applet's JNLP file as shown in the following code snippet:
<applet-desc main-class="SayHello" name="main test" height="150" width="300"> <param name="java_decorated_frame" value="true" /> <param name="java_applet_title" value="" /> </applet-desc>
Download source code for the Draggable Applet example to experiment further.