Running Applets

Main Page

An applet is a little Java program that runs inside a Web browser. The purpose of an applet is to extend the functionality of a Web page in a browser. The HTML document tells the browser to load and run an applet using the HTML tag <applet>. You can write and test your applet in Eclipse. This page will explain how you run an applet, and how to pass parameters to the applet, as if the applet were being called by a browser.

This tutorial assumes you have downloaded the Eclipse project Welcome and imported it (as a .jar file) into Eclipse. (See import a jar for how.)

Running an applet

  1. Your Eclipse workspace should look as follows

  2. The file Welcome.java is an applet. You can see this by looking at the class Welcome defined in this file, which extends the applet class JApplet

  3. You can run the applet by right-clicking (<ctrl&rt; + Mouse-click on the Mac) the file and selecting "Run as Java Applet".

  4. This will bring-up the applet viewer

  5. You can make the applet bigger by dragging the right-corner. This applet responds to mouse-clicks. Click your mouse over the black background in the viewer a few times. You will see a message displayed where you clicked

  6. This applet also takes parameters passed by the HTML page. To see how to pass parameters to an applet, go to the next section.

Passing Parameters to an Applet

This section assumes you understand how to run applets from the previous section. An HTML page can pass parameters to an applet. To run the Welcome applet from an HTML page, you would have the following in your HTML document

<applet class="Welcome.class">
    <param name=MESSAGE value="Hello World!!">
</applet>
This particular applet displays the message "Welcome!!" as a default when no parameter was passed. This was the case in the previous section. This section will show you how to pass a parameter MESSAGE to the applet in Eclipse.

  1. Right-click (<ctrl&rt; + Mouse-click on the Mac) the file and select "Run...".

  2. You will now have the Run Wizzard

  3. Select "Java Applet" and New

  4. You will be given the following window

  5. Select "Parameters".

  6. Now select "Add", to add a parameter. You must enter MESSAGE for the "Name" field. Enter anything you like on the "Value" field. Here is what I entered

  7. You can add more parameters, although they will be ignored by this applet. When you are finished click Apply. Then click Run to run the applet. By clicking on the applets black background you will see your message


Main Page