Java Forum / General / November 2005
applet inside an applet
Baji - 22 Nov 2005 14:41 GMT Dear Friends, please suggest me a soultion in the following issue. I am new to java & please bare my poor english.
I have an appplet, inside the applet, if some conditions matches I am loading a new applet. using the follwing syntax Applet realApplet; Class appletClass = Class.forName("client.class"); realApplet = (Applet)appletClass.newInstance(); realApplet.setStub(this); setLayout( new GridLayout(1,0)); add(realApplet); realApplet.init(); realApplet.start();
now I have a method called "ClientMethod" inside the Client.class now I want to call this ClientMethod from the applet code, how can I achieve this.
supppose If I say
public String Message(String Msg) { realApplet.ClientMethod(Msg); }
is not working..
Please suggest me a good idea to achive the above.
Thanks in advance. Bajio.
zero - 22 Nov 2005 20:43 GMT > Dear Friends, > please suggest me a soultion in the following issue. I am new to [quoted text clipped - 30 lines] > Thanks in advance. > Bajio. If you're new to Java, you shouldn't be messing with reflection (which is what you're doing when trying to create realApplet in this code). I don't see a reason to create an applet inside an applet, and even if there is a legitimate reason I certainly wouldn't recommend it to someone new to Java. In fact, you shouldn't be bothering with applets at all when you're new to Java.
Important side-note: never start a method name with a capital letter. Use clientMethod instead of ClientMethod. Only class names should start with a capital.
That all being said, what you are trying to do is invoke method clientMethod on an Applet object. But, class Applet does not have a method with name clientMethod. You probably have a ClientApplet extends Applet or similar class, which does contain clientMethod. So then you need an object of class ClientApplet, not Applet.
ClientApplet realApplet = (ClientApplet)appletClass.newInstance();
realApplet.clientMethod("test");
PS: strictly speaking, if you put an applet inside one of your own components, you would need to create a complete, valid applet container - ie something that calls applet methods init, start, stop, destroy, ... at the appropriate times. I doubt you want to get into that, so you should try to find an alternative to your "applet inside an applet" approach.
Baji - 23 Nov 2005 05:05 GMT Hi, thanks for your reply, I am into Microsoft Technologies (VC++), but my project deals with a java appplet communicating to my VC++ server component. I am not so efficient in java programming, But I am very sorry if am wrong in the way i have wirtten that mail.
you are right, I have clientApplet which extends applet and is having a method called "clientMethod". even I have implemented all the methods such as init, start , stop, destroy. etc,. I am able to call all these methods of realApplet.
but I am unable to call an user defined method called "client Method". realApplet.clientMethod("test"); I dont know why, I am getting compilation errors such as undefined, if you want I can send you the error am getting.
please suggest me a solution for this.
warm regards, Baji.
zero - 23 Nov 2005 12:24 GMT "Baji" <Kalicharan.D.Baji@gmail.com> wrote in news:1132722352.451902.73770 @g49g2000cwa.googlegroups.com:
> Hi, > thanks for your reply, I am into Microsoft Technologies (VC++), but my [quoted text clipped - 16 lines] > warm regards, > Baji. Are you instantiating realApplet as an instance of ClientApplet? The exact error message would be helpful, as well as the exact code that generates the error. Specifically, I need to see the ClientApplet code (you can trim the parts that do work) and the code that instantiates the realApplet object.
Baji - 23 Nov 2005 13:50 GMT Let me explain you what exactly I am trying to do:
I am creating a dummy applet which connects to my server written in vc++. This server checks the validity of the connection(user), I can say the IP of the user. If the User is valid to view the original applet then I will instantiate the original 3rd Party applet. if the user has no access to view the applet I will give some error message.
Following is the code. I have cut shorted the code.
/********************************************/
import java.applet.Applet; import java.applet.AppletStub; import java.awt.*; import java.net.*; import java.io.*; import java.util.*;
public class DummyClass extends Applet implements Runnable, AppletStub { Thread appletThread; Applet realApplet;
public void init() { /* code for connecting to server written in vc++ , basically a socket connection */ }
public void paint(Graphics g) { /* general implimentaion */ }
public void run() {
/* get the response from the vc++ sever, read the buffer & check the validity of connection */
if(!bError && bException) /* if the user is valid */ { Class appletClass = Class.forName("Client"); /* Client is the original applet */ ClientApplet realApplet = (ClientApplet)appletClass.newInstance(); realApplet.setStub(this); setLayout( new GridLayout(1,0)); add(realApplet); realApplet.init(); realApplet.start(); } else { /* give error message */ } this.repaint(); validate(); } //end of run
public void start() { appletThread = new Thread(this); appletThread.start(); System.out.println("Inside Start"); }
public void stop() { if(realApplet != null) { realApplet = null; }
appletThread = null; }
public void appletResize( int width, int height ) { resize( width, height ); } synchronized void pause(int millis) { try { wait(millis); } catch (InterruptedException e) {} }
// There is a user defined method called "handleMessage" in the above realApplet, now // I want to call that method as stated below. I dont have the code for the realApplet, // it is of some 3 rd party.
public void handleMessage(String Msg) { if(realApplet != null) { realApplet.handleMessage("Msg"); } } }
/***********************************/ Following is the error I am getting when compiled. /***********************************/ DummyClass.java:269: cannot resolve symbol symbol : method handleMessage (java.lang.String) location: class java.applet.Applet realApplet.handleMessage("Msg");
I dont know what is going on when I call a userdefined method of the applet. I was able to call the same thing from java script in an asp pag, that time I will be loading a single applet in the asp page.
warm regards, Baji.
zero - 23 Nov 2005 16:41 GMT "Baji" <Kalicharan.D.Baji@gmail.com> wrote in news:1132753824.096040.206560 @g44g2000cwa.googlegroups.com:
> Let me explain you what exactly I am trying to do: > [quoted text clipped - 20 lines] > Thread appletThread; > Applet realApplet; <snip>
> public void run() > { <snip>
> ClientApplet realApplet = > (ClientApplet)appletClass.newInstance(); <snip>
> } //end of run <snip>
> public void handleMessage(String Msg) > { [quoted text clipped - 3 lines] > } > } <snip>
Ok, your problem now is a scope problem. If you've worked with VC++ you should already know all the scope rules, so I won't bore you with them (if you're fuzzy about the rules, look them up anywhere, they're pretty much the same for all OO languages).
In this case, you are creating the ClientApplet object inside the run() scope. But you have another realApplet object at class scope, which is an instance of Applet - NOT ClientApplet. It is this one that is used in handleMessage(). Not only is it an instance of Applet instead of ClientApplet, it is also null because it is not instantiated anywhere.
In short, remove "ClientApplet" in ClientApplet realApplet = (ClientApplet)appletClass.newInstance(); and change Applet realApplet to ClientApplet realApplet
As an aside, you might want to look into using ASP.NET, JSP, PHP, ... to avoid the "applet inside an applet" construct. The serverside scripting could check the user's credentials, and if he has permission, insert the <applet> tag into the html. If he doesn't have permission, insert an error message instead of the applet. This could still use your vc++ server, or it could use the scripting language's own authentication methods.
Roedy Green - 23 Nov 2005 16:57 GMT >Ok, your problem now is a scope problem. If you've worked with VC++ you >should already know all the scope rules, so I won't bore you with them (if >you're fuzzy about the rules, look them up anywhere, they're pretty much >the same for all OO languages). see http://mindprod.com/jgloss/scope.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Baji - 24 Nov 2005 04:22 GMT Thanks for your Suggestions. Let me try that, will update you again. but why you are suggesting me to user some alternative for the "applet insdie an applet construct"
Andrew Thompson - 24 Nov 2005 14:29 GMT > Thanks for your Suggestions. Let me try that, will update you again. > but why you are suggesting me to user some alternative for the "applet > insdie an applet construct" It is good advice. GUI'd programs, and then Applets, are not easy, and many people unfamiliar with Java get into all sorts of troubles when writing them. <http://www.physci.org/codes/javafaq.jsp#appfirst>
Hosting an applet inside another applet, (Reflection aside) is harder again by an order of magnitude.
Ultimately, you can achieve much the same effect by having the first applet use AppletContext.showDocument(URL) to go to the second applet (on a different page).
Note that if you do not want to do that because the user sees the 'secret' URL, you are pretty much lost anyway, as the (Java aware) end user could discover the second applet, and write a simple page of their own to call and use it.
 Signature Andrew Thompson physci, javasaver, 1point1c, lensescapes - athompson.info/andrew Currently accepting short and long term contracts - on Earth.
Baji - 25 Nov 2005 07:11 GMT Hi Andrew, Thanks for your mail. My main concern with the security, user should not be able to know anything abou the realApplet. One more thing is yesterday I tried the things you have explained about the scope probhlem, but I was unable to solve the probhlem, I am getting the same error. I am trying for the same. Please let me know if you have anyother valuable suggestions.
Thanks & Regards, Baji.
Andrew Thompson - 25 Nov 2005 08:42 GMT > Hi Andrew, > Thanks for your mail. Please make note of a minor but important distinction.
What I made was a 'post' to a usenet newsgroup, which is archived all over the place, and publicly browsable. Further, I offered the advice for free.
If it had been a surface mail or e-mail, it would not be archived or publicly browsable in the usenet newsgroups. ..And I'd have charged for it.
> .. My main concern with the security, user > should not be able to know anything abou the realApplet. There is no way to do that.
> ...Please let me know if you have anyother > valuable suggestions. Rethink the design.
 Signature Andrew Thompson physci, javasaver, 1point1c, lensescapes - athompson.info/andrew Currently accepting short and long term contracts - on Earth.
Baji - 25 Nov 2005 13:31 GMT Thank you very much Andrew.
Cheers, Baji.
> > Hi Andrew, > > Thanks for your mail. [quoted text clipped - 4 lines] > is archived all over the place, and publicly browsable. > Further, I offered the advice for free.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|