The
basic applet
Java has the ability to create applets, which are little programs that run inside a Web browser. Because they must be safe, applets are limited in what they can accomplish. However, applets are a powerful tool that support client-side programming, a major issue for the Web. Feedback
Applet
restrictions
Programming within an applet is so restrictive that its often referred to as being inside the sandbox, since you always have someonethat is, the Java run-time security systemwatching over you. Feedback
However, you can also step outside the sandbox and write regular applications rather than applets, in which case you can access the other features of your OS. Weve been writing regular applications all along in this book, but theyve been console applications without any graphical components. Swing can be used to build GUI interfaces for regular applications. Feedback
You can generally answer the question of what an applet is able to do by looking at what it is supposed to do: extend the functionality of a Web page in a browser. Since, as a Net surfer, you never really know if a Web page is from a friendly place or not, you want any code that it runs to be safe. So the biggest restrictions youll notice are probably: Feedback
- An applet cant touch the local disk. This means writing or reading, since you wouldnt want an applet to read and transmit private information over the Internet without your permission. Writing is prevented, of course, since that would be an open invitation to a virus. Java offers digital signing for applets. Many applet restrictions are relaxed when you choose to allow signed applets (those signed by a trusted source) to have access to your machine. Youll see an example later in this chapter, as well as an example of Java Web Start, a way to safely send applications to a client over the Internet. Feedback
- Applets can take longer to display, since you must download the whole thing every time, including a separate server hit for each different class. Your browser can cache the applet, but there are no guarantees. Because of this, you should always package your applets in a JAR (Java ARchive) file that combines all the applet components (including other .class files as well as images and sounds) together into a single compressed file that can be downloaded in a single server transaction. Digital signing is available for each individual entry in the JAR file. Feedback
Applet advantages
If you can live within the restrictions, applets have definite advantages, especially when building client/server or other networked applications: Feedback
- There is no installation issue. An applet has true platform independence (including the ability to easily play audio files, etc.), so you dont need to make any changes in your code for different platforms, nor does anyone have to perform any installation tweaking. In fact, installation is automatic every time the user loads a Web page that contains applets, so updates happen silently and automatically. In traditional client/server systems, building and installing a new version of the client software is often a nightmare. Feedback
- You dont have to worry about bad code causing damage to someones system, because of the security built into the core Java language and applet structure. This, along with the previous point, makes Java useful for so-called intranet client/server applications that live only within a company or restricted arena of operation where the user environment (Web browser and add-ins) can be specified and/or controlled. Feedback
Because applets are automatically integrated with HTML, you have a built-in platform-independent documentation system to support the applet. Its an interesting twist, since were used to having the documentation part of the program rather than vice versa. Feedback
Application frameworks
Libraries are often grouped according to their functionality. Some libraries, for example, are used as is, off the shelf. The standard Java library String and ArrayList classes are examples of these. Other libraries are designed specifically as building blocks to create other classes. A certain category of library is the application framework, whose goal is to help you build applications by providing a class or set of classes that produces the basic behavior that you need in every application of a particular type. Then, to customize the behavior to your own needs, you inherit from the application class and override the methods of interest. The application frameworks default control mechanism will call your overridden methods at the appropriate time. An application framework is a good example of separating the things that change from the things that stay the same, since it attempts to localize all the unique parts of a program in the overridden methods.[76] Feedback
Applets are built using an application framework. You inherit from class JApplet and override the appropriate methods. There are a few methods that control the creation and execution of an applet on a Web page:
|
Method |
Operation |
|---|---|
|
init( ) |
Automatically called to perform first-time initialization of the applet, including component layout. Youll always override this method. |
|
start( ) |
Called every time the applet moves into sight on the Web browser to allow the applet to start up its normal operations (especially those that are shut off by stop( )). Also called after init( ). |
|
stop( ) |
Called every time the applet moves out of sight on the Web browser to allow the applet to shut off expensive operations. Also called right before destroy( ). |
|
destroy( ) |
Called when the applet is being unloaded from the page to perform final release of resources when the applet is no longer used |
With this information you are ready to create a simple applet:
//: c14:Applet1.java
// Very simple applet.
import javax.swing.*;
import java.awt.*;
public class Applet1 extends JApplet {
public void init() {
getContentPane().add(new JLabel("Applet!"));
}
} ///:~Note that applets are not required to have a main( ). Thats all wired into the application framework; you put any startup code in init( ). Feedback
In this program, the only activity is putting a text label on the applet, via the JLabel class (the old AWT appropriated the name Label as well as other names of components, so you will often see a leading J used with Swing components). The constructor for this class takes a String and uses it to create the label. In the preceding program this label is placed on the form. Feedback
The init( ) method is responsible for putting all the components on the form using the add( ) method. You might think that you ought to be able to simply call add( ) by itself, and in fact thats the way it used to be in the old AWT. However, Swing requires that you add all components to the content pane of a form, so you must call getContentPane( ) as part of the add( ) process. Feedback



RSS feed Java FAQ News