Share this page 

Have Applets on the same page communicate with each other (this howto is deprecated)Tag(s): DEPRECATED


[Applet1_0022.java]
import java.awt.*;
public class Applet1_0022 extends java.applet.Applet {
  TextField inputText;
  Button    b;

  public void init() {
    setLayout(new FlowLayout());
    inputText = new TextField( "", 15 );
    b = new Button("Send to Applet 2");
    add(inputText);
    add(b);
  }

  // JDK 1.02 style evant handling
  public boolean action(Event ev, Object arg) {
    if (ev.target instanceof Button) {
       String textMsg = inputText.getText().trim();
       Applet2_0022 applet2 =
         (Applet2_0022)getAppletContext().getApplet("applet2");
       if ( applet2 != null ) {
          applet2.append( textMsg );
          return true;
       }
       else {
          System.out.println("Applet2 not found?");
          return false;
       }
     }
     return false;
  }
}
[Applet2_0022.java]
import java.awt.*;

public class Applet2_0022 extends java.applet.Applet {
  TextArea textBox;

  public void init() {
    setLayout(new FlowLayout());
    textBox = new TextArea( 5, 40 );
    add( textBox );
  }

  public void append( String msg ) {
    textBox.append( msg );
    textBox.append( "\n" );
  }
}
[HTML]
<HTML><HEAD></HEAD><BODY>
<APPLET CODE="Applet1_0022.class"
        NAME="applet1"
        HEIGHT=200 WIDTH=150>
</APPLET>
<APPLET CODE="Applet2_0022.class"
        NAME="applet2"
        HEIGHT=200 WIDTH=400>
</APPLET>
</BODY></HEAD>
Try it here.