Andreas Rozek Hints for Reading List of Recent Changes Guestbook Entry Contact the Author  Deutsche Version  HomePage Previous Topic Next Topic  First Page of Current Topic Next Page Previous Page

Applet_03

Multiple 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_03 (Context Display) snapshot

Applet_03 (Applet Information Display) snapshot

Applet_03 (Parameter Information Display) snapshot

The three individual applets on the left side all belong together: apart from being derived from the same basic source code (and distinguished by their name only) they also "interact" with each other.

Click on an entry in the "List of available Applets" and see, how "Applet Information" and "Parameter Information" of that applet appear in the other two windows.

These examples don't work within a Netscape browser: a number of bugs (only one of them could be compensated by a "workaround") prevent an applet from contacting its neighbours. As a consequence you'll see the "Context Display" only. Try running this applet triple in an appletviewer in order to get an example that works as foreseen.

Applet Names

As 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 Context

Applets 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 Interaction

The 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 Code

The source code of this applet is available for download:

Disclaimer

Please, consider also the author's Disclaimer!

http://www.Andreas-Rozek.de/Java/JavaKurs/Applet_03/Applet_03.html    (last Modification: 01.05.2002)