![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This section coversJApplet
a class that enables applets to use Swing components.JApplet
is a subclass ofjava.applet.Applet
, which is covered in the Writing Applets
trail. If you've never written a regular applet before, we urge you to read that trail before proceeding with this section. The information provided in that trail applies to Swing applets, with a few exceptions that this section explains.
Any applet that contains Swing components must be implemented with a subclass of
JApplet
. Here's a Swing version of one of the applets that helped make Java famous an animation applet that (in its most well known configuration) shows our mascot Duke doing cartwheels:
You can find the main source code for this applet in
Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the J2SE JRE or SDK. We strongly recommend that you install the latest version; at least 1.3.1 is required for all our applets. You can find more information in the Java Plug-in home page.TumbleItem.java
. See the examples index for links to all the files required by this example.
This section discusses the following topics:
BecauseJApplet
is a top-level Swing container, each Swing applet has a root pane. The most noticeable effects of the root pane's presence are support for adding a menu bar and the need to use a content pane.As described in Using Top-Level Containers, each top-level container such as a
JApplet
has a single content pane. The content pane makes Swing applets different from regular applets in the following ways:
- You add components to a Swing applet's content pane, not directly to the applet. Adding Components to the Content Pane shows you how.
- You set the layout manager on a Swing applet's content pane, not directly on the applet.
- The default layout manager for a Swing applet's content pane is
BorderLayout
. This differs from the default layout manager forApplet
, which isFlowLayout
.- You should not put painting code directly in a
JApplet
object. See Converting to Swingfor information on converting applet painting code, and Working with Graphics
for examples of how to perform custom painting in applets.
Because applets inherently use multiple threads and Swing components aren't thread safe, you should take care with threads in Swing applets. It's generally considered safe to create and manipulate Swing components directly in theinit
method. However, the other milestone methods start
,stop
, anddestroy
might cause trouble when the browser invokes them after the applet's already visible. To avoid trouble, you should make these methods thread safe.For example, when you implement a
stop
orstart
method, be aware that the browser doesn't call them from the event-dispatching thread. Thus, those methods shouldn't affect or query Swing components directly. Instead, they should use techniques such as using theSwingUtilities.invokeLater
method to affect components.For more information about using threads, see Threads and Swing
and How to Use Threads
.
TheApplet
class provides thegetImage
method for loading images into an applet. ThegetImage
method creates and returns anImage
object that represents the loaded image. Because Swing components useIcon
s rather thanImage
s to refer to pictures, Swing applets tend not to usegetImage
. Instead Swing applets create instances ofImageIcon
an icon loaded from an image file.ImageIcon
comes with a code-saving benefit: it handles image tracking automatically. Refer to How to Use Iconsfor more information.
The animation of Duke doing cartwheels requires 17 different pictures. The applet uses one
ImageIcon
per picture and loads them in itsinit
method. Because images can take a long time to load, the icons are loaded in a separate thread implemented by aSwingWorker
object. Here's the code:
public void init() { ... imgs = new ImageIcon[nimgs]; ... final SwingWorker worker = new SwingWorker() { public Object construct() { //Images are numbered 1 to nimgs, //but fill array from 0 to nimgs-1. for (int i = 0; i < nimgs; i++) { imgs[i] = createFrame(i+1); } finishedLoading = true; return imgs; } ... }; worker.start(); } protected ImageIcon createFrame(int imageNum) { String path = dir + "/T" + imageNum + ".gif"; URL imgURL = this.getClass().getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } }
The recommended way to include an applet in an HTML page is using the APPLET tag. (Before version 1.3.1_01a, Java Plug-in required the OBJECT or EMBED tag instead. Details are available through the Java Plug-in home page.) Here's the APPLET tag for the cartwheeling Duke applet:
To find out about the various <APPLET> tag parameters, refer to Test Driving an Applet<applet code="TumbleItem.class" codebase="example-1dot4/" archive="tumbleClasses.jar tumbleImages.jar" width="600" height="95"> <param name="maxwidth" value="120"> <param name="nimgs" value="17"> <param name="offset" value="-57"> <param name="img" value="images/tumble"> Your browser is completely ignoring the <APPLET> tag! </applet>and Using the APPLET Tag
.
The next table lists the interesting methods thatJApplet
adds to the applet API. They give you access to features provided by the root pane. Other methods you might use are defined by theComponent
and
Applet
classes. See Component Methods for a list of commonly used
Component
methods, and Taking Advantage of the Applet APIfor help in using
Applet
methods.
Method Purpose void setContentPane(Container)
Container getContentPane()
Set or get the applet's content pane. The content pane contains the applet's visible GUI components and should be opaque. JRootPane createRootPane()
void setRootPane(JRootPane)
JRootPane getRootPane()
Create, set, or get the applet's root pane. The root pane manages the interior of the applet including the content pane, the glass pane, and so on. void setJMenuBar(JMenuBar)
JMenuBar getJMenuBar()
Set or get the applet's menu bar to manage a set of menus for the frame. void setGlassPane(Component)
Component getGlassPane()
Set or get the applet's glass pane. You can use the glass pane to intercept mouse events. void setLayeredPane(JLayeredPane)
JLayeredPane getLayeredPane()
Set or get the applet's layered pane. You can use the frame's layered pane to put components on top of or behind other components.
This table shows examples of Swing applets and where those examples are described.
Example Where Described Notes TumbleItem
This page An animation applet HelloSwingApplet
Running Swing Applets The simplest of all Swing applets it contains only a label. AppletDemo
Running Swing Applets The applet version of the button demo program. Can be run either as an application or as an applet. IconDemoApplet
How to Use Icons An applet for showing photos. Several examples. Using Layout Managers You can run several applets from the page listed. Each applet demonstrates a different layout manager. Several examples. Some Simple Event-Handling Examples You can run several applets from the page listed. Each applet shows how to handle different types of events.
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2003 Sun Microsystems, Inc. All rights reserved.