Java Student Project         


Titles
Ability Level: Intermediate
Estimated Time: 45 minutes
Objectives: · Learn how to write an HTML document to host a Java applet

· Learn how to write a simple applet that uses a text Label

· Be able to change the properties of a Label

· Understand event-driven programming

· Be able to add Text Fields and Buttons to the applet

· Learn how to make an applet respond with output

Materials & Resources:
  • Java Software
  • Textbook
Overview: The basic form of the <APPLET> tag is:

<APPLET CODE=YourApplet.class WIDTH=integerValue HEIGHT=integerValue> </APPLET>

This tag tells the browser to load the applet whose class file is named YourApplet.class, displaying it in an area of the specified width and height. You may specify parameters in the APPLET tag to customize the applet's configuration, for example pass the applet a text string to be used for a button's caption. You specify the value of a parameter using a <PARAM> tag. The <PARAM> tags should appear just after the <APPLET> tag for the applet they affect:

<APPLET CODE=yourApplet.class WIDTH=400 HEIGHT=200>
<PARAM NAME=param1 VALUE=someValue>
<PARAM NAME=param2 VALUE=someValue>
</APPLET>

Here's an example of the <PARAM> tag in use.

<APPLET CODE="yourApplet.class" WIDTH=400 HEIGHT=200>
<PARAM NAME="imageName" VALUE="images/Beans">
<PARAM NAME="background" VALUE="0xc0c0c0">
</APPLET>

You can specify alternate text to Java-enabled browsers and other browsers that understand the <APPLET> tag by using the ALT attribute. If the browser can't display an applet, it can display the applet's ALT text.

<APPLET CODE="YourApplet.class" WIDTH=400 HEIGHT=200 ALT="You are unable to run this applet"> </APPLET>

A browser that doesn't understand the <APPLET> tag will ignore everything inside the opening and closing APPLET tags. In this case, you can simply place any text you want to appear before the closing APPLET tag, and the browser that isn't capable of dealing with the applet will at least display that text. There are many older browsers still in use, and this should be taken into account when writing applications for the public World Wide Web. In the case of an intranet application, you have control over the browser in use, and this is not an issue.

By default, a browser looks for an applet's class files in the same directory as the HTML file that has the <APPLET> tag. In some cases you may need to put the applet's files somewhere else. You can use the CODEBASE tag to tell the browser where the applet's files are located:

<APPLET CODE=YourApplet.class CODEBASE="http;//mysite.com/applets" WIDTH=400 HEIGHT=200></APPLET>

You can load an applet from your own server or from any server that you have access to on the internet. If your applet has more than one file, you can archive all the applet's files into a single archive file. In order to speed download time, and reduce the number of HTTP requests required to retrieve the applet files. The JAR archive format, the standard Java archive format, was introduced in JDK 1.1 and is based on the ZIP file format. You use the ARCHIVE attribute of the <APPLET> tag to specify one or more archive files:

<APPLET CODE="YourApplet.class" ARCHIVE="archive1" WIDTH=400 HEIGHT=200></APPLET>

Instructions: Create an applet that displays an employee's title in a TextField when the user types an employee's first and last names (separated by a space) in another TextField, or displays an employee's name in a TextField when the user types an employee's title in a TextField. Include a Label for each of the TextFields. Add a Label at the top of the applet with the text "Enter a name or a title". You can use arrays to store the employees' names and titles.
Hints: Some important points to keep in mind in doing this project:

I. Writing an HTML document to host an applet

    A. Java applications are stand-alone programs, Java applets must be run from another program, either the appletviewer program provided in the Java SDK or a web browser that is Java-enabled.

    B. An applet must be called from within an HTML document. HTML is Hypertext Markup Language, a simple tag-based language used by web browsers to display pages.

    C. To create an applet, you must

        1. Write the applet in the Java language, save it with the .java extension.

        2. Compile the applet into bytecode using the javac compiler.

        3. Write an HTML document that includes an <APPLET> tag to call the applet.

        4. Load that HTML file into appletviewer or a web browser to call and display the applet.

    D. A web browser is a program that allows display of documents and images. The most common browsers at this time are Netscape Navigator or Communicator, and Microsoft Internet Explorer.

    E. HTML contains many tags that allow you to format text and graphics on a web page, and also provide links to other pages. All HTML tags have an opening and closing tag that are identical except that the closing tag is preceeded by a forward slash, as in <B> </B> for the bold tag which will cause text to be displayed in bolded text.

    F. The basic <APPLET> tag has the format:

<APPLET CODE="AClass.class" WIDTH=300 HEIGHT=200>

        1. The CODE attribute tells the name of the compiled applet you are calling.

        2. The WIDTH is the width in pixels on the screen.

        3. The HEIGHT is the height in pixels.

II. Writing a simple applet using a label

    A. Most applets contain at least two import statements: import.java.applet.*; and import.java.awt.*; Every applet is based on the java Applet class. The AWT package is the Abstract Windowing Toolkit and contains classes for Labels, Buttons and other GUI components.

    B. An applet isn't required to use Windows style GUI componenets, but almost always does.

    C. Applets and their components, are event-driven. The applet is loaded by the web browser, and the the applet automatically calls a number of methods when events occur. If you do not provide these methods in your applet, they will be created for you.

        1. The init( ) method is the first method in any applet and is used to perform initialization tasks.

        2. The start( ) method is called by init( ) and subsequently any time the web page is reactivated.

        3. The stop( ) method is called is a user leaves a web page containing an applet.

        4. The destroy( ) method is called when the browser or appletviewer is closed.

    D. The applet AWT components have methods that may be called to change their appearance and location on the screen. They also may respond to user events such as a mouse click or a change in focus.

III. Event-driven programming

    A. Events occur when someone using your applet takes some action, such as clicking on a Button object.

    B. In procedural programs, the programmer dictates a number of processes to occur in a certain order. In event-driven programs, events are generated randomly, and any number of events may occur in any order.

    C. Java AWT objects that are interested in events are designated as listeners. Not all objects can receive events. Objects must be registered in the applet as listeners in order to receive events.

    D. To respond to events in an applet you must:

        1. Prepare the applet to accept event messages by adding the phrase implements ActionListener to the class header, and import the package java.awt.event.* ActionListener is an interface, or set of specifications for methods that can be used with events.

        2. Tell the applet to expect events to happen by using the addActionListener( ) method with any object you want to receive events, or become a "target" for events.

        3. Tell the object how to respond to the event by writing the actionPerformed( ) method to implement the action.

IV. Adding output to an Applet

    A. Output in an applet may be provided by adding a Label object when it is required to display some text.

    B. Objects may be added or removed from applets with the add( ) and remove( ) methods dynamically.

    C. After adding or removing components, you must force the applet to repaint itself by calling the invalidate( ) and validate( ) methods.

Extra: