SDSU CS 596 Java Programming
Fall Semester, 1998
Communicating Applets
To Applet Index
San Diego State University -- This page last updated 16-Dec-98

Description

This applet shows how two applets on the same page can communicate with each other. Show two methods of finding other applets on the page. Finding an applet via getApplet( "applet name") worked in the appletviewer, but not with IE 3.01a on a PowerMac

Applet

Your browser does not support Java applets, so you can not see this applet operate.

Applet Tag

<DIV ALIGN="center">
   <APPLET 
      ARCHIVE="AppletClasses.jar" 
      CODE="ReceiverApplet.class" 
      WIDTH=100 HEIGHT=100 
      NAME="pete">
   </APPLET>
   <APPLET 
      ARCHIVE="AppletClasses.jar" 
      CODE="ReceiverApplet.class" 
      WIDTH=100 HEIGHT=100 
      NAME="sam">
   </APPLET>
   <APPLET 
      ARCHIVE="AppletClasses.jar" 
      CODE="SenderApplet.class" 
      WIDTH=100 HEIGHT=100>
      Your browser does not support Java applets, so
      you can not see this applet operate.
   </APPLET> 
</DIV>

Applet Source Code

ReceiverApplet.java

import java.applet.*;
import java.awt.Graphics;
import java.awt.Dimension;

public class ReceiverApplet extends Applet 
  {
  String message = "Not set";
  
  public void paint( Graphics display ) 
    {
    display.drawString( message, 20, 20 );

    // -1 to account for width of line
    Dimension size = getSize();
    display.drawRect( 0, 0, size.width-1, size.height-1);
    }

  public void setMessage( String newMessage ) 
    {
    message = newMessage;
    repaint();
    }
  }

SenderApplet.java

import java.applet.*;
import java.util.Enumeration;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.Button;

public class SenderApplet extends Applet 
  {
  public void init( ) 
    {
    Button sam = new Button( "Sam" );
    add( sam );
    sam.addActionListener( new SamListener() );
    
    Button doIt = new Button( "Do it" );
    add( doIt );
    doIt.addActionListener( new DoItListener() );
    }

  class SamListener implements ActionListener 
    {
    public void actionPerformed( ActionEvent event )  
      {
      // Communication using applet name set in HTML tag
      AppletContext myContext = getAppletContext();
      ReceiverApplet partner;
      partner = (ReceiverApplet) myContext.getApplet( "sam" );
      
      if ( partner == null )
        getAppletContext().showStatus( "Can't find Sam" );
      else
        partner.setMessage( "Hi mom" );
      }
    }//Class  DoItListener        

  class DoItListener implements ActionListener 
    {
    public void actionPerformed( ActionEvent event )  
      {
      // Show how to find applet in list of all applets in page
      Enumeration applets = getAppletContext().getApplets();
      
      while ( applets.hasMoreElements() ) 
        {
        Object applet = applets.nextElement();
        if ( applet instanceof ReceiverApplet ) 
          ((ReceiverApplet) applet).setMessage( "Did it");
        }
      }
    }//Class  DoItListener        

  }


Visitors since 16-Dec-98