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

Please help me for Applet Communications!

 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic