Dewi
asked on
JApplet link to JApplet
I have a project, built by Visual Cafe 3.0, How to link 2 applet or more
i.e :
I have JApplet a and JAppletb, at the first time, user will get JApplet a, and by the time the JButton on JApplet a, being click, it will open JApplet b in the same browser window ?
i.e :
I have JApplet a and JAppletb, at the first time, user will get JApplet a, and by the time the JButton on JApplet a, being click, it will open JApplet b in the same browser window ?
Fully working example follows. Uses Applet class but just use JApplets instead in your case.
Full details are here:
http://www.netwave.net/members/raji/sb/iactop.htm
Shows how to transfer data between two applets with a working example here:
http://www.netwave.net/members/raji/sb/iacapplet.htm
Uses three applets. Source, target and middle class to communicate between them.
The middle class is used for a specific reason as explained on the pages above...though you can do it without it.
Use this html:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>IAC Applet page</title>
</head>
<frameset framespacing="0" border="false" frameborder="0" cols="*,*">
<frame name="left" src="iacmain.htm" scrolling="auto">
<frame name="right" src="subframe.htm" scrolling="auto">
<noframes>
<body>
<p>This page uses frames, but your browser doesn't support them.</p>
</body>
</noframes>
</frameset>
</html>
and compile these three applets:
*
* Applet
* Author: D.L.Sivakumar
* Date: 16 Nov. 1997
*
* This source code is published in the internet for education
* purpose only. Author shall not be liable any damages suffered
* by user as a result of using, modifying or distributing
* this source code.
*
* Function: This applet is a Source Applet in Inter-Applet Communication.
* It uses MiddleGuy class to set the value into a static variable
* which is read by the Target Applet.
*/
import java.awt.*;
import java.applet.*;
import java.net.*;
import MiddleGuy;
public class MainApp extends Applet
{
void button1_Clicked(Event event)
{
try
{
MiddleGuy.setTextData(txtN ame.getTex t());
AppletContext ac = getAppletContext();
URL u = new URL(getDocumentBase(), "subframe.htm");
ac.showDocument(u, "right");
}
catch(MalformedURLExceptio n e)
{
showStatus("Error " + e);
}
}
public void init()
{
super.init();
setLayout(null);
addNotify();
resize(435,239);
button1 = new java.awt.Button("Send Text to Target Applet");
button1.reshape(120,168,23 6,32);
add(button1);
txtName = new java.awt.TextField(25);
txtName.reshape(168,84,157 ,24);
add(txtName);
lblName = new java.awt.Label("Enter Text");
lblName.reshape(60,84,79,2 3);
add(lblName);
label1 = new java.awt.Label("Source Applet",Label.CENTER);
label1.reshape(48,12,367,3 7);
label1.setFont(new Font("Dialog", Font.BOLD, 16));
add(label1);
}
public boolean handleEvent(Event event)
{
if (event.target == button1 && event.id == Event.ACTION_EVENT) {
button1_Clicked(event);
return true;
}
return super.handleEvent(event);
}
java.awt.Button button1;
java.awt.TextField txtName;
java.awt.Label lblName;
java.awt.Label label1;
}
*
* Applet
* Author: D.L.Sivakumar
* Date: 16 Nov. 1997
*
* This source code is published in the internet for education
* purpose only. Author shall not be liable any damages suffered
* by user as a result of using, modifying or distributing
* this source code.
*
* Function: This applet is a Target Applet in Inter-Applet Communication.
* It uses MiddleGuy class to get the value from the static variable
* which is set by the Source Applet.
*/
import java.awt.*;
import java.applet.*;
import MiddleGuy;
public class SubApp extends Applet {
public void init() {
super.init();
setLayout(null);
addNotify();
resize(426,266);
lbl_sname = new java.awt.Label("Target Field");
lbl_sname.reshape(48,120,1 10,23);
add(lbl_sname);
txt_sname = new java.awt.TextField(20);
txt_sname.setEditable(fals e);
txt_sname.reshape(180,120, 200,25);
add(txt_sname);
label1 = new java.awt.Label("Target Applet",Label.CENTER);
label1.reshape(36,24,367,3 7);
label1.setFont(new Font("Dialog", Font.BOLD, 16));
add(label1);
/* Call to the Middle Guy class method directly as this method is defined as static method. */
txt_sname.setText(MiddleGu y.getTextD ata());
}
public boolean handleEvent(Event event) {
return super.handleEvent(event);
}
java.awt.Label lbl_sname;
java.awt.TextField txt_sname;
java.awt.Label label1;
}
*
* Applet
* Author: D.L.Sivakumar
* Date: 16 Nov. 1997
*
* This source code is published in the internet for education
* purpose only. Author shall not be liable any damages suffered
* by user as a result of using, modifying or distributing
* this source code.
*
* Function: This is a Middle class used in Inter-Applet Communication.
* It uses aa static variable which is used by the Source and the
* Target Applet.
*/
public class MiddleGuy
{
public static void setTextData(String str)
{
txt = str;
}
public static String getTextData()
{
return txt;
}
static String txt;
}
And hey presto...
Full details are here:
http://www.netwave.net/members/raji/sb/iactop.htm
Shows how to transfer data between two applets with a working example here:
http://www.netwave.net/members/raji/sb/iacapplet.htm
Uses three applets. Source, target and middle class to communicate between them.
The middle class is used for a specific reason as explained on the pages above...though you can do it without it.
Use this html:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>IAC Applet page</title>
</head>
<frameset framespacing="0" border="false" frameborder="0" cols="*,*">
<frame name="left" src="iacmain.htm" scrolling="auto">
<frame name="right" src="subframe.htm" scrolling="auto">
<noframes>
<body>
<p>This page uses frames, but your browser doesn't support them.</p>
</body>
</noframes>
</frameset>
</html>
and compile these three applets:
*
* Applet
* Author: D.L.Sivakumar
* Date: 16 Nov. 1997
*
* This source code is published in the internet for education
* purpose only. Author shall not be liable any damages suffered
* by user as a result of using, modifying or distributing
* this source code.
*
* Function: This applet is a Source Applet in Inter-Applet Communication.
* It uses MiddleGuy class to set the value into a static variable
* which is read by the Target Applet.
*/
import java.awt.*;
import java.applet.*;
import java.net.*;
import MiddleGuy;
public class MainApp extends Applet
{
void button1_Clicked(Event event)
{
try
{
MiddleGuy.setTextData(txtN
AppletContext ac = getAppletContext();
URL u = new URL(getDocumentBase(), "subframe.htm");
ac.showDocument(u, "right");
}
catch(MalformedURLExceptio
{
showStatus("Error " + e);
}
}
public void init()
{
super.init();
setLayout(null);
addNotify();
resize(435,239);
button1 = new java.awt.Button("Send Text to Target Applet");
button1.reshape(120,168,23
add(button1);
txtName = new java.awt.TextField(25);
txtName.reshape(168,84,157
add(txtName);
lblName = new java.awt.Label("Enter Text");
lblName.reshape(60,84,79,2
add(lblName);
label1 = new java.awt.Label("Source Applet",Label.CENTER);
label1.reshape(48,12,367,3
label1.setFont(new Font("Dialog", Font.BOLD, 16));
add(label1);
}
public boolean handleEvent(Event event)
{
if (event.target == button1 && event.id == Event.ACTION_EVENT) {
button1_Clicked(event);
return true;
}
return super.handleEvent(event);
}
java.awt.Button button1;
java.awt.TextField txtName;
java.awt.Label lblName;
java.awt.Label label1;
}
*
* Applet
* Author: D.L.Sivakumar
* Date: 16 Nov. 1997
*
* This source code is published in the internet for education
* purpose only. Author shall not be liable any damages suffered
* by user as a result of using, modifying or distributing
* this source code.
*
* Function: This applet is a Target Applet in Inter-Applet Communication.
* It uses MiddleGuy class to get the value from the static variable
* which is set by the Source Applet.
*/
import java.awt.*;
import java.applet.*;
import MiddleGuy;
public class SubApp extends Applet {
public void init() {
super.init();
setLayout(null);
addNotify();
resize(426,266);
lbl_sname = new java.awt.Label("Target Field");
lbl_sname.reshape(48,120,1
add(lbl_sname);
txt_sname = new java.awt.TextField(20);
txt_sname.setEditable(fals
txt_sname.reshape(180,120,
add(txt_sname);
label1 = new java.awt.Label("Target Applet",Label.CENTER);
label1.reshape(36,24,367,3
label1.setFont(new Font("Dialog", Font.BOLD, 16));
add(label1);
/* Call to the Middle Guy class method directly as this method is defined as static method. */
txt_sname.setText(MiddleGu
}
public boolean handleEvent(Event event) {
return super.handleEvent(event);
}
java.awt.Label lbl_sname;
java.awt.TextField txt_sname;
java.awt.Label label1;
}
*
* Applet
* Author: D.L.Sivakumar
* Date: 16 Nov. 1997
*
* This source code is published in the internet for education
* purpose only. Author shall not be liable any damages suffered
* by user as a result of using, modifying or distributing
* this source code.
*
* Function: This is a Middle class used in Inter-Applet Communication.
* It uses aa static variable which is used by the Source and the
* Target Applet.
*/
public class MiddleGuy
{
public static void setTextData(String str)
{
txt = str;
}
public static String getTextData()
{
return txt;
}
static String txt;
}
And hey presto...
Hi Dewi,
Applet to Applet contest will work only when all the applets are in same web page ( There may be posibility that they may be reside in different frames also).
With out frames and with out same page, there should be no communication between applets .
If you want that functionaliy,
instead of writing multiple applets, write multiple panels with card layout and display the required card according to the program logic.
The above answer of jod will allow applets to communicate if there are in different frames but in a same browser window.
So cardlayout with different panels will be better than multiple applets and passing values between them
Best of luck.
ASKER
Jod, sorry I have to reject your answer, since It is not work (I'm not using frames at all) btw thanks for your links
ravindra76, thanks for your suggestion
If you want this Points, You can take it else I'll delete this Q
ravindra76, thanks for your suggestion
If you want this Points, You can take it else I'll delete this Q
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Jod, sorry I have to reject your answer, since It is not work
It definately works and you can see it working on the above links as well as other more advanced examples for communicating between applets...but perhaps it is not two applets you actually need as ravindra suggests.
If JApplet a and JApplet b do not need to run concurrently and JAppleta only runs JApplet b and then exits then different layouts of the same applet are more appropriate.
Good luck...
It definately works and you can see it working on the above links as well as other more advanced examples for communicating between applets...but perhaps it is not two applets you actually need as ravindra suggests.
If JApplet a and JApplet b do not need to run concurrently and JAppleta only runs JApplet b and then exits then different layouts of the same applet are more appropriate.
Good luck...
ASKER
I mean, I know how to do that (using getAppletContext().showDoc
but the problem I want to pass some parameters from JApplet a to JApplet b