Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 101: An alternative way for applet-to-applet communication

Learn how to let your applets speak across frames and browser window boundaries

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 3

The fact that the runtime environment is shared will make static fields and structures accessible for all applet instances, and therefore, those static fields and structures can be used to pass information between different applets.

Not only can you let simple data types like integers, chars, and strings be stored, but you can also let each applet store a reference to itself (to the instance) in a static field (possibly within its own class). Any other applet will be able to access that field, getting the reference to the instance.

Does that sound tricky? Well, it is not. I'll start with an easy example. Suppose you have one applet (AppletA.class) in one frame and another applet (AppletB.class) in another frame, and both applets are loaded from the same codebase.

You now want to give AppletA access to AppletB's public methods. You need to make AppletB save a reference to itself in a static and public field, like this:

    public class AppletB {
        public static AppletB selfRef = null; // Initially zero
        public void init() {
            //  Making reference to the instance
            selfRef = this;
        }
        ...
    }


From AppletA you can now access the instance of AppletB:

    public class AppletA {
        AppletB theOtherApplet = null;
        public void callAppletB() {
            // Getting the static field where the pointer
            // to the instance of AppletB is stored.
            theOtherApplet = AppletB.selfRef;
            // After this it is possible to call instance
            // methods, like this...
            theOtherApplet.repaint();
        }
        ...
    }


That is all there is to it. That method works even though the applets are not on the same page because the runtime environment is shared between different applets.

It is important to note that the code above does not deal with the case where the callAppletB method in AppletA is called before AppletB has started. If that happens, the selfRef will be null and no communication can take place.

A more generic approach

Of course, there is a more generic approach. You can create a class whose only purpose is to store references to applets in a static data structure. An example of such a class, called AppletList, can be seen below. Applet instances that want to give other applets access to their public methods register with the AppletList. By following the pattern in AppletContext.getApplet(string name), each registration has to be associated with a string. The string then serves as a key when another applet calls for a reference to a certain applet.

Typically, an applet registers like this:

    public class AppletA {
        public void start() {
            AppletList.register("Stock-trade-applet", this);
            ...
        }
    }


Then another applet gets access to it:

    public class AppletB {
        public void run() {
            AppletA tradeApplet =
                (AppletA) AppletList.getApplet("Stock-trade-applet");
            ...
        }
    }


You must also remember to unregister from the AppletList when the applet stops:

    public void stop() {
        AppletList.remove("Stock-trade-applet");
        ...
    }


The complete source for the AppletList class looks like this:

0: import java.util.*;
1: import java.applet.Applet;
2:
3: public class AppletList {
4:    private static Hashtable applets = new Hashtable();
5:
6:    public static void register(String name, Applet applet) {
7:          applets.put(name,applet);
8:    }
9:
10:   public static void remove(String name) {
11:         applets.remove(name);
12:   }
13:
14:   public static Applet getApplet(String name) {
15:         return (Applet) applets.get(name);
16:   }
17:
18:   public static Enumeration getApplets() {
19:         return applets.elements();
20:   }
21:
22:   public static int size() {
23:         return applets.size();
24:   }
25: }


Please download the exampleCode.zip file in Resources for examples of how to use this class.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources