|
Have Applets on the same page communicate with each other |
|
|
The applets on the same page can communicate with each other. The following example shows how to do this:
The following code snippet shows the source code of the first applet:
import java.awt.*;
public class Applet1 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);
}
public boolean action(Event ev, Object arg) {
if (ev.target instanceof Button) {
String textMsg = inputText.getText().trim();
Applet2 applet2 =
(Applet2)getAppletContext().getApplet("applet2");
if ( applet2 != null ) {
applet2.AppendText( textMsg );
return true;
}
else
return false;
}
return false;
}
|
The following code snippet shows the source code of the second applet:
import java.awt.*;
public class Applet2 extends java.applet.Applet {
TextArea textBox;
public void init() {
setLayout(new FlowLayout());
textBox = new TextArea( 5, 40 );
add( textBox );
}
public void AppendText( String msg ) {
String newLine = new String( "\n" );
textBox.appendText( msg );
textBox.appendText( newLine );
}
}
|
The HTML source code of the page containing applets is as follows:
<HTML><HEAD></HEAD><BODY>
<APPLET CODE="Applet1.class"
NAME="applet1"
HEIGHT=200 WIDTH=150>
</APPLET>
<APPLET CODE="Applet2.class"
NAME="applet2"
HEIGHT=200 WIDTH=400>
</APPLET>
</BODY></HEAD>
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.