Java Classes

ClassRegistry
Connecting applets in different frames and documents


NOTE: The current release version of ClassRegistry is available on the NACSE home page, at <URL:http://www.nacse.org /web/webterm/>. The version on this page is currently under development, and not supported.

One problem we have run into with Java is trying to send commands and data between two or more currently executing applets. While it is theoretically possible to do this in a single document by retrieving the document's list of applets through the java.applet.AppletContext.getApplets() method, there appears to be no easy way to get a handle on applets executing in different frames or windows.

The ClassRegistry was created to address this need. It works by defining a class with static methods for registering objects and sending messages to them. All objects using the same ClassRegistry class share this information, so that commands can be sent to objects existing in different applet contexts.

We have available a simple demo showing the registry in action.

Programming with the ClassRegistry

To use the ClassRegistry in your own program, you need to do two things:

  1. Create a class that implements a ClassConnection interface. Instances of this class may then be registered as an arbitrary name with the ClassRegistry by calling its addClass() method.

  2. In another class, call the registry's doCommand() method to send a command to the objects in the registry, using the same name as above.

The demo available on this page uses two simple applets that implement this functionality.

Downloading the ClassRegistry

To use the ClassRegistry in your own code, just retrieve the source code as a compressed (18K) or gzipped (10K) tar file; this includes compiled binaries, the demo HTML files, and source code with comments explaining how to use the registry.

Implementation classes

ClassRegistry

The actual class that keeps track of registered objects. The class should never be instantiated; instead, it is used by calling the static methods of the class, to register objects with it and send messages to the currently registered objects.

Because the class's methods and data are static, they are shared between all objects that use the class: thus an object in one applet or document can "see" the objects registered by another.

Note that, for this to work, your objects must all be accessing the same class: i.e., they must use the registry at the same URL. The practical implication of this is that the applets that wish to communcate with each should be stored in the same directory on your server, along with the actually ClassRegistry class.

RegistryButton

An applet implementing a simple button that sends a message to a ClassRegistry every time it is pressed by the user. There are four parameters used to control its behavior:

REGISTER
A string defining the registry name to send the command to. The default is "DemoApplet".
LABEL
A string defining the text to draw on the button's face. The default is "Demo".
COMMAND
A string with the name of the command to send to the registry. The default is "send".
TEXT
A string with the data to send to the registry. The default is "Demo".

RegistryDemo

Implements a simple applet for the demo. All it does is create a TextArea and register itself with a ClassRegistry, then it displays information about the commands sent to it by the registry.

Implementation interfaces

ClassConnection

The interface that must be implemented by any object that wishes to register itself with the ClassRegistry.


Dianne Kyra Hackborn <hackbod@cs.orst.edu>

Last modified: Mon Jul 22 17:55:39 PDT 1996