Saturday, April 26, 2008

Applet to Applet Communication

The AppletContext class in the java.applet package contains the two member methods getApplet and getApplets. By using those methods, an applet can find other applets and invoke methods on them.
That can only be done if the following security requirements are met:

  • The applets originate from the same server and from the same directory on the server.
  • The applets are running on the same page and in the same browser window.

Those security restrictions may have been designed that way for good reason; however, the latter requirement limits the ways you can make interesting multiapplet interfaces featuring applet-to-applet (a2a) communication.

Using the AppletContext API
Before I explain the alternative a2a-communication mechanism, I will briefly show how the getApplet and getApplets methods work. An applet can find another applet in the same HTML page by either using the getApplet method to look it up by name or using the getApplets method to find all the applets on the page.

Both methods, if successful, return one or more Applet objects to the caller. Once the caller finds an Applet object, it can invoke the Applet's public methods.

Suppose that a snippet of the HTML page looks like this:

<applet code="Applet1" width="400" height="100" name="app1"> </applet> <br> <applet code="Applet2" width="400" height="100" name="app2"> </applet> <br>

By using the name attribute in the applet tag, you can refer to a specific applet in the following way:

Applet theOtherApplet = getAppletContext().getApplet("app1");
theOtherApplet.anyMethod(); //calling any public method

Or, we can use the following code to retrieve all applets on the page:

Enumeration allAppletsOnSamePage = getAppletContext().getApplets();
while(allAppletsOnSamePage.hasMoreElements()) {
Applet appl = (Applet) allAppletsOnSamePage.nextElement();
appl.anyMethod(); //Calling any public method
}

When the calling applet has retrieved one or several applets on the same HTML page, it can call those applets' public methods.

Example:
First Applet Program
//One.java
import javax.swing.*;
import java.awt.*;
public class One extends JApplet
{
JTextArea taOutput;
public void init()
{ getContentPane().setLayout( new FlowLayout());
taOutput = new JTextArea(5,20);
getContentPane().add(taOutput);
}

public void putText(String str)
{
taOutput.append( str + "\n");
}
}
Second Applet Program:
//Two.java
import javax.swing.*;
import java.awt.event.*;
import java.applet.AppletContext;
import java.awt.*;
public class Two extends JApplet implements ActionListener{
JTextField txtInput;
AppletContext ac;
JButton btnSend;
public void init()
{
getContentPane().setLayout( new FlowLayout());
txtInput = new JTextField(25);

getContentPane().add(txtInput);
btnSend = new JButton("Send");

getContentPane().add(btnSend);
btnSend.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
ac = getAppletContext();
One obj = (One) ac.getApplet("One");
if(obj != null)
{
obj.putText(txtInput.getText());
txtInput.setText("");
} }}
AppletToApplet.html file

<HTML> <HEAD> <TITLE> Applet-Applet Communicaton </TITLE> </HEAD>
<BODY BGCOLOR="#FFFFFF"> <h1> Applet-Applet Communicaton </h1>
<applet code="Two" width="400" height="300" name="Two"> </applet>
<br> <hr> <br>
<applet code="One" width="400" height="300" name="One"> </applet>

</BODY> </HTML>

  • Compile Both Program
  • Run the Program
Output 1:

Output after Click on Send Button


Conclusion:
So we can easily communicate one applet to another applet by using AppletContext with following restrictions:

  • The applets originate from the same server and from the same directory on the server.
  • The applets are running on the same page and in the same browser window

No comments: