Tutorial 09: AWT Applet


This tutorial covers:

Applets are Java programs that are uploaded from the Web server and run on the client machine inside of a Java-enabled Web browser.  We shall restrict ourselves to working only with Java's AWT (abstract window toolkit) components instead of Swing because most current browsers do not fully support Swing applets.  Just about all the Swing components would have their own AWT counterparts.  For example the AWT counterpart for JButton is Button.  Refer to the UML diagram for Java GUI component in lab #04 to see where AWT components belong in the taxonomy tree.

I. Sample Program

Create a local directory comp212/tutorials/09 for this lab and copy the files ~/comp212/tutorials/09/*.java and ~/comp212/tutorials/09/AppletOnly.html.  These are the Java source code and html document for an applet that manipulates LRStruct.  Compile the Java source and then browse AppletOnly.html to see the applet run.  Play around with this applet to see how it behaves.  The program is designed according to the MVC pattern as shown in the UML diagram below.

appletOnly.png (23048 bytes)

In the above diagram, the model is the familiar LRStruct.   ListGUI is the view.  The controller, ControlApplet, is a subclass of java.awt.Applet.  It overrides the init () method to instantiate the view and the model, and add action listeners to the view's GUI components in order to interact with external users and update the view accordingly.

II. Basic Applet MVC Design



public ListGUI (Applet applet)
    applet.setLayout (new BorderLayout (10, 10));
    applet.add (makeViewPanel(), BorderLayout.CENTER);
    applet.add (makeUserInputPanel(), "West");


You view an applet by using a Java-enabled browser to open an html document that contains the appropriate applet tag.  You can also use a Java utility that comes with the JDK called appletViewer to view and test an Applet by executing the command: appletViewer <html-file-name>.


III. Applet/Application Combination

It is often desirable to have a Java program that can run both as an Applet and as a stand-alone application.  There are many ways to achieve this goal.  The following is a possible approach.



Exercise: Modify the given ControlApplet.java and ListGUI.java to run both as an applet and as a stand-alone GUI application by carrying out the steps outlined in section III.


revised 03/27/00