The getApplet method
looks through all of the applets on the current page
to see if one of them has the specified name.
If so, getApplet
returns the applet's Applet object.
By default, an applet has no name.
For an applet to have a name,
one must be specified in the HTML code
that adds the applet to a page.
You can specify an applet's name in two ways:
- By specifying a
NAME attribute within the applet's <APPLET> tag.
For example:
<APPLET CODEBASE=example/ CODE=Sender.class WIDTH=450 HEIGHT=200
NAME="buddy">
. . .
</applet>
- By specifying a
NAME parameter with a <PARAM> tag.
For example:
<APPLET CODEBASE=example/ CODE=Receiver.class WIDTH=450 HEIGHT=35>
<PARAM NAME="name" value="old pal">
. . .
</applet>
Browser Note:
Although at least one Java-enabled browser
conducts a case-sensitive search,
the expected behavior is for the
getApplet method
to perform a case-insensitive search.
For example, getApplet("old pal")
and getApplet("OLD PAL") should both find an
applet named "Old Pal".
Below are two applets that illustrate lookup by name.
The first, the Sender, looks up the second, the Receiver.
When the Sender finds the Receiver,
the Sender sends a message to the Receiver
by invoking one of the Receiver's methods
(passing the Sender's name as an argument).
The Receiver reacts to this method call
by changing its leftmost text string
to "Received message from sender-name!".

This figure has been reduced to fit on the page.
Click the image to view it at its natural size.
Note:
Because some old browsers don't support 1.1,
the above applets are 1.0 versions.
Here are links to the 1.0 code for the
Sender and
Receiver classes.
To run the 1.1 versions of the applets,
go to example-1dot1/iac.html.
Try this:
Click the Send message button of the top applet (the Sender).
Some status information will appear in the Sender's window,
and the Receiver will confirm (with its own status string)
that it received a message,
After you've read the Receiver status string,
press the Receiver's Clear button
to reset the Receiver.
In the Sender's text field labeled "Receiver name:,"
type in buddy and press Return.
Since "buddy" is the Sender's own name,
the Sender will find an applet named buddy
but won't send it a message,
since it isn't a Receiver instance.
Here's the
whole Sender program.
The code it uses to look up
and communicate with the Receiver
is listed below.
Code that you can use without change
in your own applet
is in bold font.
Applet receiver = null;
String receiverName = nameField.getText(); //Get name to search for.
receiver = getAppletContext().getApplet(receiverName);
The Sender goes on to make sure
that the Receiver was found
and that it's an instance of the correct class (Receiver).
If all goes well,
the Sender sends a message to the Receiver.
(Here's the
Receiver program.)
if (receiver != null) {
//Use the instanceof operator to make sure the applet
//we found is a Receiver object.
if (!(receiver instanceof Receiver)) {
status.appendText("Found applet named "
+ receiverName + ", "
+ "but it's not a Receiver object.\n");
} else {
status.appendText("Found applet named "
+ receiverName + ".\n"
+ " Sending message to it.\n");
//Cast the receiver to be a Receiver object
//(instead of just an Applet object) so that the
//compiler will let us call a Receiver method.
((Receiver)receiver).processRequestFrom(myName);
}
} . . .
From an applet's point of view,
its name is stored in a parameter named NAME.
It can get the value of the parameter
using the Applet getParameter method.
For example, Sender gets its own name with the following code:
myName = getParameter("NAME");
For more information on using getParameter, see
Writing the Code to Support Parameters.
The example applets in this page perform one-way communication --
from the Sender to the Receiver.
If you want your receiver to be able to send messages to the sender,
then you just need to have the sender give a reference to itself
(this) to the receiver.
For example:
((Receiver)receiver).startCommunicating(this);