CS 117 Lab: Applets

For this lab, you'll take one of the programs you've already written and transform it into an applet. Then, you'll build a non-javabook applet of your own.

Lab 0: Transform a program to an applet

1. Open up a project corresponding to your favorite Java program. Remember that applets don't let you do file or terminal input/output, so choose a program that doesn't do these things (or take a program that does and remove them).

2. Add a new class to your project called MyApplet. Make sure to click on "Applet" when you create the class.

3. Copy the code from your "main" to the "init" method of the applet.

4. An applet will not correctly close your mainWindow, outputBox, etc. because your program never really "exits". It keeps on running within the web browser. Therefore, for each of your windows, you should call the "dispose" method at the very end of your program. For example, in my CD library, I add the following statements of code to the end of my "start" method within my CompactDiscLibrary class:

mainWindow.dispose();
outputBox.dispose();

5. Compile your program class and the applet

6. Right click the applet, and choose "Run Applet". Choose "Run Applet in appletviewer" at the top (should be default), and click "OK". Your program should run as it usually does.

7. Change your program to not show your MainWindow, if you like.

8. Find the html and class files generated by BlueJ for your project. Copy them to your web directory as described at

        http://www.student.carleton.edu/builders/web_pages.html

9. Find the javabook library at http://www.drcaffeine.com/student/javabook.htm, and download to your webpage. Make sure to choose the AWT based javabook. Double-click on the zip file after downloading to unzip to a "javabook" folder within your webpage folder.

10. Start up Netscape, try your webpage, and see if your applet runs! There does appear to be a bug with the combination of Netscape, MultiInputBoxes, and javabook. If you're having trouble seeing an entire MultiInputBox, try it again with Internet Explorer.
 

Lab 1: Non-javabook Applet

You'll find below a sample applet, similar to the one we did in class. Modify one of your old simple programs, such as ChangeMaker, to run as an applet without javabook. See if you can get it working on your web page. Good luck!
 

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Greeting extends Applet implements ActionListener
{

    private Label prompt;
    private Label greeting;
    private TextField inputLine;

    public Greeting()
    {
        prompt = new Label("Please enter your name:");
        greeting = new Label();
        inputLine = new TextField(15);

        add(prompt);
        add(inputLine);

        inputLine.addActionListener(this);
 
        Button button;
        button = new Button("Answer me!");
        add(button);
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event)
    {
        String inputText = inputLine.getText();
        greeting.setText("Welcome, " + inputText + "!");
        add(greeting);
        doLayout();
    }

}