Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making two applets talk

 
Carlos Sancini
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've made a simplified set of my project that has the problem as follows:
There are two diferent applets that are packaged in two diferents jars that must be shown in the same page and must talk. One applet implements an interface and, the other one, searches the first and needs to access the methods from its implemented interface. The problem is that the second applet can't access the methods from the implemented interface: the expression (Applet1 instanceof Interface1) is false even after have tested the implemented interfaces in runtime (with getClass().getInterfaces()). This problem doesn't happen when the class files are not packaged (but I need because they must be signed).
The jars are sender.jar(Applet2.class Applet2$1.class Interface1.class)
receiver.jar(Applet1.class Interface1.class)

Here is the code for the html page:
#######################################################################
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>

<table border="2">
<tr>
<td><strong>Applet1 (Receiver)</strong></td></tr>
<tr>
<td><applet name="applet1" height="100" width="400"
archive="receiver.jar"
code="Applet1.class"></applet></td></tr>
<tr>
<td><strong>Applet2 (Sender)</strong><td></td></tr>
<tr>
<td><applet name="applet2" height="100" width="400"
archive="sender.jar"
code="Applet2.class"></applet></td></tr>
</table>

</body>
</html>
#######################################################################

Here is the code for the first Applet
#######################################################################
public class Applet1 extends Applet implements Interface1 {

private TextArea txtarStatus;
private Label lblMessage;
private String newline;

public void displayData(String data) {
txtarStatus.append(data + newline);
}

public void init(){

newline = System.getProperty("line.separator");
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

setLayout(gridBag);

Label lblMessage = new Label("Received:", Label.RIGHT);
gridBag.setConstraints(lblMessage, c);
add(lblMessage);

txtarStatus = new TextArea(5, 60);
txtarStatus.setEditable(false);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(txtarStatus, c);
add(txtarStatus);
this.setSize(400,200);
}

}
#######################################################################

Here is the code for the second Applet
#######################################################################
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Applet2 extends Applet {

private TextField txtNameField;
private TextArea txtarStatus;
private String newline;

public void init(){

newline = System.getProperty("line.separator");
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
Label lblMessage = new Label("Message:", Label.RIGHT);
gridBag.setConstraints(lblMessage, c);
add(lblMessage);
txtNameField = new TextField("", 10);
c.fill = GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(txtNameField, c);
add(txtNameField);
Button button = new Button("Send");
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.NONE;
gridBag.setConstraints(button, c);
add(button);
button.addActionListener(al);
txtarStatus = new TextArea(5, 60);
txtarStatus.setEditable(false);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(txtarStatus, c);
add(txtarStatus);
}

ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent event) {
Applet receiver = null;
receiver = getAppletContext().getApplet("applet1");

if (receiver != null) {
//Just to show that Applet2 implements Interface1
txtarStatus.append("## Applet2 -> " +
receiver.getClass().getInterfaces()[0] +
" ##" + newline);
if (!(receiver instanceof Interface1)) {
txtarStatus.append("Does not implement Interface1." +
newline);
} else {
txtarStatus.append("Implements Interface1 -> " +
"sending message" + newline);
((Interface1)receiver).displayData(txtNameField.getText());
}
} else {
txtarStatus.append("Applet2 not found." + newline);
}
}
};
}
#######################################################################

Here is the code for interface
#######################################################################
public interface Interface1 {
public void displayData(String data);
}
#######################################################################

I am compiling the classes together and packanging with ant

Here is the build.xml
#######################################################################
<?xml version="1.0" encoding="UTF-8"?>
<project default="packApplets">

<target name="packApplets">
<jar jarfile="jars/receiver.jar"
basedir="bin/"
includes="Applet1.class Interface1.class"
excludes="Applet2.class Applet2$1.class"/>
<jar jarfile="jars/sender.jar"
basedir="bin/"
includes="Applet2.class Applet2$1.class
Interface1.class"
excludes="Applet1.class "/>
</target>

</project>
#######################################################################

Cab anyone help me???

Carlos.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic