Share this page 

Have Applets on different frames communicates with each other (this howto is deprecated)Tag(s): DEPRECATED


It is possible to share data between different applets via static variables

[HTML (java-0023.html)]
<HTML><HEAD></HEAD>
<FRAMESET COLS="50%,*">
    <FRAME SRC="java-0023-f1.html" NAME="f1">
    <FRAME SRC="java-0023-f2.html" NAME="f2">
</FRAMESET>
</HEAD>
[HTML (java-0023-f1.html AND java-0023-f2.html)]
<HTML><HEAD></HEAD>
<BODY>
<APPLET CODE="Java0023.class"
        HEIGHT=200
        WIDTH=200>
</APPLET>
</BODY></HTML>
[JAVA source (Java0023.java)]
import java.awt.*;
import java.applet.*;

public class Java0023 extends Applet {
  TextField tf;
  Button    a,b;

  public void init() {
    setLayout(new FlowLayout());
    a  = new Button("Send to Message");
    b  = new Button("Receive from Message");
    add(a);
    add(b);
    tf = new TextField(20);
    add(tf);
  }

  // JDK 1.02 style evant handling
  public boolean action(Event e, Object o) {
    if (e.target instanceof Button) {
       if (e.target == a) {
          StaticMessage.message = tf.getText();
       }
       if (e.target == b) {
          tf.setText(StaticMessage.message);
       }
       return true;
    }
    return false;
  }
}

class StaticMessage {
  public static String message = "";
}

Try it here.

Check this DDJ article for a more elaborate way to implement this principle.

There is one way to do it by using a Javascript function as a bridge, take a look at this How-to.