Tutorial 11: AWT Applets


Introduction

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 components  to see where AWT components belong in the taxonomy tree.


I. Sample Program

Create a local directory comp212/tutorials/11 for this lab and copy the files in ~/comp212/tutorials/11/AppletOnly.  These are the Java (incomplete) source code, fully working byte code, and html document for an applet that manipulates LRStruct.  DO NOT compile the Java source because ControlApplet.java is incomplete.   Use Netscape (or IE) to 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 (21302 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

Model

View

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

Controller

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>.

Exercises

  1. Write the action listener code for the Sum and the GetNth buttons.
  2. Add a button to remove the Nth element from the list model.  The input for N should be read from the input text field.  You will need to write a visitor to remove the Nth element.


dxnguyen@cs.rice.edu

revised 11/20/00