aspose file tools*
The moose likes Applets and the fly likes CALLING   RAUKAUTAM SANDEEP,ANGELA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "CALLING   RAUKAUTAM SANDEEP,ANGELA" Watch "CALLING   RAUKAUTAM SANDEEP,ANGELA" New topic
Author

CALLING RAUKAUTAM SANDEEP,ANGELA

asheet anand
Ranch Hand

Joined: Nov 15, 2000
Posts: 83
hi,
angela asked a ? for applet to applet communication on 28th nov,
2000 and sandeep you answered the ?.
thanks alot it was a great help to me.IT WORKS FINE ALSO.
but i have a problem,in your example you have both the
applets defined in one HTML file.what I did is ,i defined the
to applets in two different HTML files.then from the 1st applet
i am calling the 2nd applet by showDocument() method.
but the value is not coming in the second applet.
please help me everybody.
CODE FOR 1ST APPLET
-------------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class appl1 extends Applet implements ActionListener
{
TextField t1,t2;
Button b1,b2;
AppletContext ac;
txtctrl txt;
public void init()
{
t1=new TextField(10);
t2=new TextField(10);
b1=new Button("Add");
b2=new Button("Exit");
add(t1);
add(t2);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
ac=getAppletContext();
txt=(txtctrl)ac.getApplet("textboxapplet");
}
public void actionPerformed(ActionEvent ae)
{
int txt1,txt2,txt3;
if(ae.getSource().equals(b1))
{
String f1,f2,f3;
f1=(t1.getText());
f2=(t2.getText());
f3=f1+f2;
txt.tf.setText(f3);
URL u=getCodeBase();
try{
ac.showDocument(new URL(u+"go1.html"));
}catch(Exception e){}

showStatus("You have clicked the Add button in the applet window.");
}
else if(ae.getSource().equals(b2))
{
t1.setText("hi");
}
}
}
CODE FOR 2ND APPLET
---------------------
import java.awt.*;
import java.applet.*;
public class txtctrl extends Applet
{TextField tf;

public void init()
{tf=new TextField(10);
add(tf);
tf.setEnabled(false);
}
}
HTML FILE FOR 1ST APPLET.
-------------------------
<html>
<BODY>
<APPLET CODE="appl1.class" name="sampleapplet" width=250 height=100>
</APPLET>
</BODY>
</HTML>

HTML FILE FOR 2ND APPLET
---------------------------
<html>
<BODY>
<APPLET CODE="txtctrl.class" name="textboxapplet" width=250 height=100></APPLET>
</BODY>
</HTML>
all the files are in the same directory.
PLEASE HELP AS I AM REALLY STUCK UPPPPP.
THANKS IN ADVANCE,
asheet
Nitin Shivaram
Ranch Hand

Joined: Jan 17, 2001
Posts: 71
Hi asheet,
Well thats the limitation of AppletContext.getApplet(s), it returns only the applets which are on the same html page.
There is a workaround to this, and this is what you have to do.
Each of the applets you are using should have a static field in it which stores a reference to itself.
Say CallingApplet has to call CalledApplet for some functionality.
CalledApplet stores a reference to itself in its init method.

Now the calling applet CallingApplet will get the reference of the CalledApplet by doing this..

The above workaround works because applets having the same codebase will share the same runtime environment, and likewise the static variables.
cheerio !!

------------------
Nitin S


Nitin S<br />Sun Certified Java Programmer for the Java 2 platform.<br />Tekmetrics Certified Java Programmer For the Java 2 Platform.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CALLING RAUKAUTAM SANDEEP,ANGELA