| Andreas Rozek |
|
Applet_03Multiple applets may reside on the same web page and interact with each other. "Applet_03" shows how to examine an applet's context and interact with other applets. Please, also consider my "Hints for Reading" and the "List of Recent Changes"!
Applet NamesAs mentioned above, the three instances of the same applet distinguish themselves by their "names". An applet "name" can be specified as part of an <applet> directive as shown below:
<applet code=... width=... height=... name="applet_name">
parameters and alternate HTML code
</applet>
Inside an applet, the name can be obtained like an "ordinary" parameter:
String AppletName = getParameter("Name");
This is also true for all other parameters (such as Code, Width or Height), the spelling of parameter names is not case-sensitive. Applet ContextApplets run within a context (usually provided by a web browser or an applet viewer), which is shared among all applets in the same HTML document. Using that context, an applet is able to load bitmap images and audio clips, instruct the browser to show another HTML document or contact neighbouring applets - the latter is demonstrated in this example. An applet's context may be obtained using AppletContext Context = getAppletContext(); Using this Context, it is now possible to either look for a particular applet by its "name" Applet AppletInstance = Context.getApplet(AppletName); or to obtain an Enumeration of all available applets (including the current one)
Enumeration AppletSet = Context.getApplets();
while (AppletSet.hasMoreElements()) {
Applet Neighbour = (Applet) AppletSet.nextElement();
String Name = Neighbour.getParameter("Name");
...
};
Applet-Applet InteractionThe abovementioned context methods return objects of type Applet, which are instances of all currently active applets. As with any other object, it is now possible to invoke methods on these instances - either standard ones (like getParameter, f.e.) or others that were introduced by applet subclasses. In that case, however, you have to "cast" the objects to their original types first - but then you have full access to all fields of that class:
Applet AppletInstance = getApplet(AppletName); // get selected applet
Applet_03 InfoDisplay = (Applet_03) getApplet("Applet Information Display");
if (InfoDisplay != null) { // display some info in the appropriate place
InfoDisplay.AppletArea.setText(AppletInstance.getAppletInfo());
};
Source CodeThe source code of this applet is available for download:
|
| http://www.Andreas-Rozek.de/Java/JavaKurs/Applet_03/Applet_03.html | (last Modification: 01.05.2002) |