aspose file tools*
The moose likes Applets and the fly likes Please help me for Applet Communications! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Please help me for Applet Communications!" Watch "Please help me for Applet Communications!" New topic
Author

Please help me for Applet Communications!

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
I want to know whether I can call one applet from another applet, if yes, how?(Please explain with very simple example)
Second, if i want to make database connections means if I have to use GET and POST methods for server and client connection or database connection, Can I Use applet or I have to write Applications only?
Thanks in advance,
Angela Jessi
Raukutam Sandeep
Ranch Hand

Joined: Nov 15, 2000
Posts: 61
hi angela,

applet to applet communication is possible.
u have to use getAppletConext() and getApplet() methods to get this fucntionality. I will give a simple example, where u have two textfields and a button in one applet, whenever u enter the values in the textfield and click on the button, the addition result is shown on the textfield present in the second applet.
First applet program:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
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))
{
txt1=Integer.parseInt(t1.getText());
txt2=Integer.parseInt(t2.getText());
txt3=txt1+txt2;
txt.tf.setText(String.valueOf(txt3));
//showStatus("You have clicked the Add button in the applet window.");
}
else if(ae.getSource().equals(b2))
{
//destroy();
}
}
}
In tha above program, the parameter that is passed for the getApplet() method is the name of the applet that u mention in the <applet> tag.
Second applet program:
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);
}
}
here is the applet tags in html program:
<APPLET CODE="appl1.class" name="sampleapplet" width=250 height=100>
</APPLET>
<APPLET CODE="txtctrl.class" name="textboxapplet" width=250 height=100></APPLET>
The name of the applet in the second applet tag, should be given as the argument in the getApplet() method as shown earlier.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi Raukutam Sandeep,
Thanks a lot!!! I really appreciate your response. I have one confusion only:When you declare txtctrl txt; is it initiation of applet txtctrl???
Please let me know,
again Thanks alot!!
Regds
Angela

Originally posted by Raukutam Sandeep:
hi angela,

applet to applet communication is possible.
u have to use getAppletConext() and getApplet() methods to get this fucntionality. I will give a simple example, where u have two textfields and a button in one applet, whenever u enter the values in the textfield and click on the button, the addition result is shown on the textfield present in the second applet.
First applet program:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
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))
{
txt1=Integer.parseInt(t1.getText());
txt2=Integer.parseInt(t2.getText());
txt3=txt1+txt2;
txt.tf.setText(String.valueOf(txt3));
//showStatus("You have clicked the Add button in the applet window.");
}
else if(ae.getSource().equals(b2))
{
//destroy();
}
}
}
In tha above program, the parameter that is passed for the getApplet() method is the name of the applet that u mention in the <applet> tag.
Second applet program:
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);
}
}
here is the applet tags in html program:
<APPLET CODE="appl1.class" name="sampleapplet" width=250 height=100>
</APPLET>
<APPLET CODE="txtctrl.class" name="textboxapplet" width=250 height=100></APPLET>
The name of the applet in the second applet tag, should be given as the argument in the getApplet() method as shown earlier.

Raukutam Sandeep
Ranch Hand

Joined: Nov 15, 2000
Posts: 61
hi angela,

you are most welcome. In the code,
'txtctrl txt;', u are just creating an instance of the seond applet class and getting that applet, using typecasting.
like, txt=(txtctrl)ac.getApplet(<appletname> );

regds,
Sandeep.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi Raukutam Sandeep
Thanks a lot!!!
Angela

Originally posted by Raukutam Sandeep:
hi angela,

you are most welcome. In the code,
'txtctrl txt;', u are just creating an instance of the seond applet class and getting that applet, using typecasting.
like, txt=(txtctrl)ac.getApplet(<appletname> );

regds,
Sandeep.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me for Applet Communications!