This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
password
Posted by zenek on October 24, 2001 at 10:03 AM
> This is just for your info. If you are interested in knowing how two applets in a browser interacts then please keep reading > Question: How can two applets in a browser interact? or > How do we call a method in one applet from another applet in a browser window? or > How do we pass a java object from applet to another applet > in the same browser window? > Here is the answer to all these questions. > Answer: Your two applets may be in the same browser window or they may be > in different frames in the same browser window. I will explain > the solution in both cases.> STEP1: Create two applets and let us call then > InterApplet1 and InterApplet2. I will explain the > code for these applets later . > STEP2: Create two HTML files and place the two applets created > in STEP1 in them . Let us call the two HTML files as > InterApplet1.html and InterApplet2.HTML. Let us name > the applet tag in InterApplet1.html as app1 and that of in > InterApplet2.html as app2. > > STEP3: Create another HTML file and put two frames in it. Let us name > these two frames as f1 and f2. The source file for f1 frame is > InterApplet1.html and for f2 it is InterApplet2.html. Let us call this > HTML file as InterApplet.html. > STEP4: Utill now we have just created a html document with two > frames in it named as f1 and f2 and placed the two applets > in them named app1 and app2 respectively. > STEP5: Create a class in java called Args. You can name it anything you > want. I just called it Agrs. This Args class has an instance > variable of String type which will hold a text. User > will type in a text in applet1 displayed in frame1 and click > a button. On button click I will read the text typed by user > then create an object of Args class and set the instance variable > equal to the text typed by the user. Then I wll get the > reference of the applet 2 displayed in frame 2 and call > passArg method in applet 2 ( In fact, InterApplet2.class ). > The passArg method of applet 2 takes one argument of type Args. > It reads the instance variable's text in Args object and then > displays the text in text area in frame 2. > STEP6: Let us discuss how do we get the reference of second applet > which is in frame 2 inside applet1 which is in frame1 > You need to use JSObject class to accomplish this. You can find > this class netscape.javascript.JSObject somewhere in windows directory > of your windows95/98 or with a .jar file in JRE installed directory > for your JDK. > We call getWindow () method of the JSObject to get the reference > of the window in which the applet resides > JSObject win = win.getWindow ( this ) ; > You need to pass the reference of the applet from which you are > calling this getWindow ( ) method. Here "this" is the reference of the > applet. > Now "win" has the reference of the window representing frame1 in our case. > Note that we are calling this method from applet1 in Frame 1. Now using > getMember () method you can get any property of this window "win" . We want to > get the reference of the top level window of the browser. > > JSObject topWin = ( JSObject ) win.getMember ( "parent" ) ; > > Note that getMember () method returns an Object so we need to cast it. Note the > also the user of "parent" property for Frame1 . Now we can get the reference of Frame2 > as > JSObject frame2 = ( JSObject ) topWin.getMember ( "f2" ) ; > > Once you have reference of frame 2 then you can get the refernce of applet 2 as > JSObject applet2 = ( JSObject ) frame2.getMember ( "app2" ) ; > > Following code create an object to pass to applet 2 > Args args = new Args ( ) ; > // Set the parameter in the argument object > args.setMsg ( t1.getText ( ) ) ; > > // call method accepts array of Object as an argument > Object[] argsArray = { args } ; > > // Call the passMsg method in applet 2 > applet2.call( "passMsg", argsArray ) ; > > Yuo use the call () method of JSObject to call a method passing method name and arguments > as its parameter. > /*****************************************/ > If your two applets are in same frame then you can get the > reference of aplet 2 as follows. > JSObject win = JSObject.getWindow ( this ) ; > JSObject doc = ( JSObject ) win.getMember ( "document" ) ; > JSObject temp = ( JSObject ) doc.getMember ( "app2" ) ; > /****************************************/ > /******************************* All the files used in this demo follows **************/ > /////////////////// InterApplet1.java //////////////////////////// > import java.applet.Applet ; > import java.awt.* ; > import java.awt.event.* ; > import netscape.javascript.JSObject ; > public class InterApplet1 extends Applet implements ActionListener{ > Button b1 = new Button ( "Click Me" ) ; > TextArea t1 = new TextArea ( 10, 40 ) ; > > public void init ( ) { > > // Add the a button and a text field to the applet > add ( b1 ) ; > add ( t1 ) ; > > // Add action listener to the button. On button clicked > // actionPerformed event will be triggered and we will > // call a method in an applet which is in another frame > b1.addActionListener ( this ) ; > } > > public String getMsg ( ) { > return "Hello, from applet 1" ; > } > > public void actionPerformed ( ActionEvent e ) { > try { > > // Create a reference of the current window in win object > JSObject win = JSObject.getWindow ( this ) ; > > // Get the reference of the parent window. It gives you the top level window > JSObject topWin = ( JSObject ) win.getMember ( "parent" ) ; > > //Get the refernce of second frame in window. We named the second frame > // as f2 > JSObject frame2 = ( JSObject ) topWin.getMember ( "f2" ) ; > > // Get the reference of the applet in frmae f2. It is named app2 > JSObject applet2 = ( JSObject ) frame2.getMember ( "app2" ) ; > > > // Create the argument object to be passed to applet 2 which is in frame 2 > Args args = new Args ( ) ; > > // Set the parameter in the argument object > args.setMsg ( t1.getText ( ) ) ; > > // call method accepts array of Object as an argument > Object[] argsArray = { args } ; > // Call the passMsg method in applet 2 > applet2.call( "passMsg", argsArray ) ; > > } catch ( Exception e1 ) { > //Handle your execptions > t1.setText ( "Exception:" + e1.getMessage ( ) ) ; > } > } > } > > //////////////////// End of InterApplet1.java///////////////////
> > /////////////////// InterApplet2.java //////////////////////////// > import java.applet.Applet ; > import java.awt.* ; > import java.awt.event.* ; > import netscape.javascript.JSObject ; > public class InterApplet2 extends Applet { > TextArea t1 = new TextArea ( 10, 40 ) ; > public void init ( ) { > add ( t1 ) ; > } > > > // Read the args message and displays in the text field > public void passMsg ( Args args ) { > t1.setText ( args.getMsg ( ) ) ; > } > } > //////////////////// End of InterApplet2.java/////////////////// > /////////////////// Args.java //////////////////////////// > public class Args { > private String msg = "Hello" ; > > public String getMsg ( ) { > return msg ; > } > > public void setMsg ( String str ) { > msg = str ; > } > } > //////////////////// End of Args.java/////////////////// > /////////////////// Inte
Replies:
|