Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Communicate between two applets  (Read 1052 times)
0 Members and 1 Guest are viewing this topic.
lokesh05
Member
*

Reputation: 0
Offline Offline
Gender: Male
Posts: 32
Referrals: 0

Awards
« on: July 02, 2008, 11:28:07 PM »

When two applets are in the same browser, they can communicate using any form of inter-object communication, including direct method calls. Communication between applets in different browsers requires some form of networking and usually an intermediate server. One common form of network communication is Remote Method Invocation (RMI).
You will occasionally need to allow two or more applets on a Web page to communicate with each other. Because the applets all run within the same Java context-that is, they are all in the same virtual machine together-applets can invoke each other's methods. The one tricky part is getting the applets in touch with each other. The AppletContext class has methods for locating another applet by name, or retrieving all the applets in the current runtime environment. Here is an example of an applet that examines all the applets in the runtime environment and displays them in a scrolling list.
Quote
    import java.applet.*;
    import java.awt.*;
    import java.util.*;

    // This applet demonstrates the use of the getApplets method to
    // get an enumeration of the current applets.

    public class ListApplets extends Applet
    {
         public void init()
         {

    // Get an enumeration all the applets in the runtime environment
              Enumeration e = getAppletContext().getApplets();

    // Create a scrolling list for the applet names
              List appList = new List();

              while (e.hasMoreElements()) {

    // Get the next applet
                   Applet app = (Applet) e.nextElement();

    // Store the name of the applet's class in the scrolling list
                   appList.addItem(app.getClass().getName());
              }

              add(appList);
         }
    }

http://img224.imageshack.us/img224/1015/lokesh051ff1.gif
Communicate between two applets

Some errors that i usually encountered were that the browser may not load applets all at once, or you might dynamically load an applet. You may receive a NullPointerException if you try to get an applet that doesn't exist-be prepared to catch it. You may have difficulty distinguishing when an applet hasn't been loaded yet from error situations in which it can't be loaded. Try picking a maximum amount of time you'll wait for an applet to be loaded, and then assume that there's a problem if the applet you want hasn't been loaded after that time.

If you already know the name of the applet you want to access, you can locate it with the getApplet method. The following code fragment locates an applet named findme:

    Applet findme = getAppletContext().getApplet("findme");
    if (findme != null) {
    // do something with findme :)
    }

You might think that the applet name you use in getApplet is the class name of the applet. This is not the case. You set the applet's name in your <APPLET> HTML tag. For example, here is the <APPLET> tag for an applet class called FindMe, which has an applet name of findme:

   
Quote
<APPLET codebase="." code="FindMe.class" name="findme">]

I would suggest you use lowercase names for naming the applets! Happy
Logged
gluk
Member
*

Reputation: 0
Offline Offline
Posts: 22
Referrals: 0

Awards
« Reply #1 on: April 21, 2009, 05:42:45 PM »

The solution for most of the Java related errors can be found at this site: Java errors and solutions.
« Last Edit: April 22, 2009, 12:47:37 PM by HappyFace » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here