Please support my sponors and make this site possible!!!
Please support our sponsors!

 

Home > Core Java FAQ > Applets FAQ
Applets
Applets Vs Applications(04)  * Installing Applets(10) * User Interface(12) * Program Structure(10)  * Applet Communication(05) * Exceptions(06) * Miscellaneous(08) 
 
Q . How can I arrange for different applets on a web page to communicate with each other?

Ans : 

Name your applets inside the APPLET tag and invoke AppletContext's getApplet method in your applet code to obtain references to the other applets on the page.

     The AppletContext interface in the java.applet package gives an applet limited access to the applet's context of execution: the browser that the applet is running in, the web page the applet is on, and other applets on the same page. You can attain a limited form of inter-applet communication by naming your applets and using the AppletContext class to find other applets by their names.
     First, you name an applet by setting the name attribute in the APPLET tag. For example, the following APPLET tags place two named applets, Kim and Sandy, on a web page:

     <applet code="ChatApplet.class" name="Kim" width=400 height=120>
     </applet>
     <applet code="QuietApplet.class" name="Sandy" width=400 height=120>
     </applet>

     Second, your applet code must query the AppletContext object to obtain references to the other applets it wants to communicate with. If you want the Sandy applet, for instance, to invoke methods on the Kim applet (such as a beQuiet method), your QuietApplet code would include lines like the following:

     /* in QuietApplet.java: */
     AppletContext context = getAppletContext();
     ChatApplet kimApplet = (ChatApplet) context.getApplet("Kim");
     kimApplet.beQuiet();

     AppletContext's getApplet method returns a reference with the type of Applet. As just shown, you need to cast that reference to the relevant Applet subclass.
     Unlike the code fragment, general-purpose applets should not hardwire specific names for other applets. Instead, they should be written to obtain the names from parameter values passed in through the APPLET tag.
     Note: Communication between applets is part of the standard Java Applet API and is supported by the JDK 1.0.2 and 1.1, but some Java-enabled browsers do not yet support it.

Q . How do I select a URL from my applet and send the browser to that page?

Ans : 

Ask the applet for its applet context and invoke showDocument on that context object.

     When an applet runs in a Java-enabled browser, it has limited access to the browser by means of the AppletContext interface. AppletContext provides two methods for requesting the browser to display new documents:

     showDocument(URL url)
     showDocument(URL url, String target)

     The one-parameter version of showDocument replaces the current web page with the one specified by the URL argument. For example:

     URL targetURL;
     String urlString = ...
     AppletContext context = getAppletContext();
     try {
          targetURL = new URL(urlString);
     } catch (MalformedURLException e) {
           // ... recover from malformed URL
     }
     context.showDocument(targetURL);

     The two-parameter version of showDocument lets you control where the new web page will appear, in terms of html "frames." html frames (not related to the AWT Frame class) subdivide a single browser window into subregions, each of which can display a web page. (Note that numerous web users regard html frames as a confusing and counterproductive addition to the hypertext model.) The String target parameter in showDocument specifies which html frame will display the new page (or whether to use a new, separate window altogether), as listed in Table 4.6.

 

Table 4.6: Controlling html Frames with showDocument
target parameter Where to show new page
"_self" the current html frame
"_parent" the parent olf the current html frame
"_top" the tomost html frame
"_blank" a new, unnamed top-level window
name a new top-level window with the specified name
Q . Can applets communicate with each other?

Ans : 

At this point in time applets may communicate with other applets running in the same virtual machine. If the applets are of the same class, they can communicate via shared static variables. If the applets are of different classes, then each will need a reference to the same class with static variables. In any case the basic idea is to pass the information back and forth through a static variable.
An applet can also get references to all other applets on the same page using the getApplets() method of java.applet.AppletContext. Once you've got a reference to an applet, you can communicate with it by using its public members.

It is conceivable to have applets in different virtual machines that talk to a server somewhere on the Internet and store any data that needs to be serialized there. Then, when another applet needs this data, it could connect to this same server. Implementing this is non-trivial. 

Q . Can applets on different pages communicate with each other?

Ans : 

No, not directly.

     In the JDK (1.0.2 and 1.1), applets can communicate directly with each other only if they are on the same web page. Otherwise, applets must arrange a meeting place at which to exchange information, either on the local file system (that is, the file system of the machine that is running the applet) or at a remote server.
     Communicating via the local file system can work in principle but is currently not a general solution. Different Java-enabled browsers vary in how, or even if, they allow applets to read and write files.
     Alternatively, your applets could communicate back to a common server, but applet security strongly restricts your options here, too. Applets loaded over the net are allowed to make network connections only to the host that served the applet's class file. You could create a server program on the host machine that tracks several applets and assists communication between them.
     Upcoming developments, such as signed applets and finer-grained security configurations, should considerably enhance the ability of applets to cooperate over the network.

Q . How can I get two applets on the same page to communicate with each other?

Ans : 

This is the purpose of the InfoBus protocol. See
http://java.sun.com/beans/infobus/index.html

The older way to do it was as follows. In your HTML page, give a NAME in the APPLET tag for the applet receiving the message, say <APPLET ... NAME=someName ...>. In the Java code of the other applet do

Applet anotherApplet = getAppletContext.getApplet("someName");

Cast anotherApplet to the correct applet subclass, and you can call any methods in the applet subclass. Don't forget to use appropriate synchronization when two threads tweak variables. This only works when
the applets are truly on the same page. If they are in different frames, it doesn't work.
You can walk through all the applets on an HTML page using code like that below. However this appears to be broken in Communicator 4.04 on Win95.

Applet otherApplet;
AppletContext ac =getAppletContext;
Enumeration applets = null;
for (applets=ac.getApplets(); applets.hasMoreElements(); ) {
   otherApplet=(Applet)applets.nextElement();
    if (otherApplet!=this) break;
        // do something with otherApplet, e.g.
    // if (otherApplet instanceof FooApplet) ...
}

Some people suggest using the static members of a common class to communicate information between the applets. This is not recommended as it relies on class-loading behavior that may change in future. Netscape changed it in one Beta so it didn't work, then changed it back again so it did. It doesn't work if you use the "mayscript" tag though. Inter-applet communication sometimes arises when you have a multi-screen type program and you don't want to force the user into downloading everything at once. One alternative is to make them into one applet with two GUIs. Try to avoid the need for applets to talk to each other. Also check the URL http://java.sun.com:81/products/hotjava/1.1/applet_environment.html
which explains how it can be done in HotJava 1.1. Recommendation: avoid code which is browser-specific.

 

Copyright © 2000 javafaq.com. All rights reserved