HTML to Applet and Applet to Applet Communication


2. Example on Applet to Applet communication

Browser permits the communication between applets. In the following program, two applets of ColorApplet are created, when a button of one applet is clicked, the background color of the other applet changes. To create two objects, we write two times the <applet> tag within the same <body> tag.

Applet file : ColorApplet.java

import java.applet.*;   
import java.awt.*;   
import java.awt.event.*;

public class ColorApplet extends Applet implements ActionListener   
{
     Button pinkBut, orangeBut, cyanBut;
     String s1= "";
     AppletContext ctx;
     ColorApplet ca;

     public void init()   
     {
	pinkBut = new Button("Pink");
	orangeBut = new Button("Orange");
	cyanBut = new Button("Cyan");

	pinkBut.addActionListener(this);
	orangeBut.addActionListener(this);
	cyanBut.addActionListener(this);

	add(pinkBut);   add(orangeBut);    add(cyanBut);
       }
       public void start()   
       {
	ctx = getAppletContext();
	ca = (ColorApplet) ctx.getApplet(getParameter("abc"));
       }  
       public void actionPerformed(ActionEvent e)   
       {
	String butLabel = e.getActionCommand();
		
	if(butLabel.equals("Pink"))             
		ca.setBackground(Color.pink);
	else if(butLabel.equals("Orange"))  
		ca.setBackground(Color.orange);
	else if(butLabel.equals("Cyan"))     
		ca.setBackground (Color.cyan);
        }
}                    

HTML file : ChangeColors.html


 
	                                



	



Applet to Applet

Screenshot of AppletToAppletDemo.html of Applet to Applet Communication

Check your browser can run this program. Some browsers do not support. Appletviewer will definitely run this program.

ctx = getAppletContext();

getAppletContext() method of Applet class returns an object of AppletContext(). The AppletContext() gets you the way to control the browser environment in which the applet or applets belonging to the same document are running.

ca = (ColorApplet) ctx.getApplet(getParameter("abc"));

getApplet() method of AppletContext returns the exact applet's execution environment which is passed as parameter. "abc" is the alias name of ColorApplet as included in the <applet> tag.

<applet> code="ColorApplet.class" width="250" height="250" name="first">
<param name="abc" value="second">
</applet>

Observe keenly the <applet> tag. Two objects of ColorApplet are created with two <applet> tags and the names are given as "first" and "second". In the first applet (whose name is "first"), we are referring the execution context of second applet (whose name is "second"). Similarly, the other way also, in the "second" applet where "second" applet refers the "first" applet. This is the place where actual link if provided between the two applets. Due to this link, if a button of one applet is clicked, the background of the other applet changes.

Note: To understand this program, knowledge of AWT Button is required.

1 thought on “HTML to Applet and Applet to Applet Communication”

Leave a Comment

Your email address will not be published.