Java Reference
In-Depth Information
Applets
23
Applets are used when a program is designed to run in a browser. Applets replace
frames in web applications. The applet is linked to an HTML page. Whenever this
HTML page is made visible in a browser, the applet is loaded over the net and runs
in the browser.
All the examples we have looked at so far have been based on frames, which are
shown on the screen. These programs, running on the local machine, are called
applications . Applets allow programs to run in a browser. Users everywhere on
the web can fetch the applet and run it on their machines. Applets are like frames
in that one can embed other graphical components into them. On the other hand
one cannot make them visible and run by themselves. They need to be linked to
an HTML page. Then if that page is displayed in a browser, the applet is started and
can be seen in the browser. Another difference between applications and applets
is that applets are not allowed to do certain things. For example an applet may
not write or read files on the machine it is running on. This is to protect users
who run an applet found on some web page on their computers. We only present
a very simple way to use applets. There are more issues the programmer should
consider when writing an applet for a serious application.
23.1
Applets in Swing
Class JApplet implements the applet concept in Swing. As mentioned above,
applets are similar to frames, because they have a content pane into which other
components can be embedded. Thus one uses the following command to embed
a component comp into an applet:
getContentPane().add(comp)
There is a difference between frames and applets. While the embedding is usually
done in the constructor of a frame, it is done in method init of the applet. Also,
an applet is not directly started by the user. Instead it is externally started by the
browser, when the corresponding web page is loaded. An applet has to be linked
to the HTML page that displays it. We shall call this page the master page of the
Search WWH ::




Custom Search