[5529] in java-interest

home help back first fref pref prev next nref lref last post

Re: Inter-applet Communication

daemon@ATHENA.MIT.EDU (Bruno Souza - Sun do Brasil)
Thu Feb 15 11:42:15 1996

Date: Mon, 12 Feb 1996 10:42:31 -0300
From: Bruno.Souza@Brazil.Sun.COM (Bruno Souza - Sun do Brasil)
To: jkap@jade.org, java-interest@java.Eng.Sun.COM

> 
> Can anyone provide (or point me toward) any information about inter-applet communication, specifically in a multi-page environment?
> 

Lot's of people has been asking this, so let me try to sumarize what
I've learned in the last weeks. (Someone that knows better, *please*
correct any mistake!!! :-)

Applets can comunicate with each other through method calling if the
applets have been loaded in the same page. I had no success so far to
make applets from different pages to comunicate (I haven't tried too
much either, since I believe it's not possible :-)

The usual way to applet1 call a method of applet2 is to applet1 grab a
handle to applet2 with AppletContext.getApplet(), do a cast to applet2
and then call it's method:

 Applet2 ap2 = (Applet2) getAppletContext().getApplet("Name_in_HTML_tag");

where "Name_in_HTML_tag" is the argument in the "name" tag on the
applet2 call ont the HTML file.

This should work, and it acctually does under appletviewer (I've tryed
both the Solaris & Windows95 version) but it *does not* work under
netscape (I've tried versions b5, b6 and final). It seems that netscape
can do AppletContext.getApplets(), but not AppletContext.getApplet().
So, the workaround that <jmarin@superlink.com> send me (and it works
beutiffuly Marin, thanks!) is to make an enumeration with
AppletContext.getApplets(), and then look for the applet that you
want:

	// get the applet context
	m_ac = getAppletContext();

	// get all applets in m_ac
	Enumeration en = m_ac.getApplets();

        for (; en.hasMoreElements() ;) 
        {
		Object o = en.nextElement();
		String name = o.getClass().getName();

		// if it is the applet I'm looking for
		if(name.equals("Applet2"))
		{
			// make a cast
			(Applet2) o;
		}
        }

	// if I got the reference to the applet
	if (o != null)
	{
		// call the method
		o.Applet2_Method(Method_Arguments);
	}

With this you don't need to use the "name" parameter in the applet tag,
because the name that you get from AppletContext.getApplets() is the
name of the class.

Hope this is not too much confusing :-) If someone does know how to
make applets that were loaded in diferent pages to comunicate in any
way (besides going back to the server to get their common info), I
would like to hear!!



Bruno.
___________________________________________________________
Bruno Peres Ferreira de Souza              Sun Microsystems
    		                 Bruno.Souza@Brazil.Sun.COM
                                     


> Thanks,
> -Jonathan
> 
> --------------------------------------------------------------------------------
> Jonathan Kaplan
> jade.org:The Java(tm) Developers Organization
> 500 Bloomfield Ave
> Verona, NJ 07044       (201) 239-7500 x264
> http://www.jade.org
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post