applet inside an applet

Discussion in 'Java' started by Baji, Nov 22, 2005.

  1. Baji

    Baji Guest

    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.
    Baji, Nov 22, 2005
    #1
    1. Advertising

  2. Baji

    zero Guest

    "Baji" <> wrote in
    news::

    > 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.
    >


    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.
    zero, Nov 22, 2005
    #2
    1. Advertising

  3. Baji

    Baji Guest

    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.
    Baji, Nov 23, 2005
    #3
  4. Baji

    zero Guest

    "Baji" <> wrote in news:1132722352.451902.73770
    @g49g2000cwa.googlegroups.com:

    > 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.
    >


    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.
    zero, Nov 23, 2005
    #4
  5. Baji

    Baji Guest

    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.
    Baji, Nov 23, 2005
    #5
  6. Baji

    zero Guest

    "Baji" <> wrote in news:1132753824.096040.206560
    @g44g2000cwa.googlegroups.com:

    > 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;
    >


    <snip>

    >
    > public void run()
    > {


    <snip>

    > ClientApplet realApplet =
    > (ClientApplet)appletClass.newInstance();


    <snip>

    > } //end of run
    >


    <snip>

    > public void handleMessage(String Msg)
    > {
    > if(realApplet != null)
    > {
    > realApplet.handleMessage("Msg");
    > }
    > }


    <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.
    zero, Nov 23, 2005
    #6
  7. Baji

    Roedy Green Guest

    On Wed, 23 Nov 2005 16:41:13 GMT, zero <> wrote, quoted or
    indirectly quoted someone who said :

    >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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 23, 2005
    #7
  8. Baji

    Baji Guest

    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"
    Baji, Nov 24, 2005
    #8
  9. Baji wrote:

    > 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.

    --
    Andrew Thompson
    physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
    Currently accepting short and long term contracts - on Earth.
    Andrew Thompson, Nov 24, 2005
    #9
  10. Baji

    Baji Guest

    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.
    Baji, Nov 25, 2005
    #10
  11. Baji wrote:

    > 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.

    --
    Andrew Thompson
    physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
    Currently accepting short and long term contracts - on Earth.
    Andrew Thompson, Nov 25, 2005
    #11
  12. Baji

    Baji Guest

    Thank you very much Andrew.

    Cheers,
    Baji.

    Andrew Thompson wrote:
    > Baji wrote:
    >
    > > 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.
    >
    Baji, Nov 25, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Eshtry
    Replies:
    0
    Views:
    613
    Andy Eshtry
    Mar 1, 2004
  2. Dave
    Replies:
    0
    Views:
    780
  3. DC Gringo
    Replies:
    4
    Views:
    4,803
    mfreidge
    Dec 17, 2007
  4. nail
    Replies:
    0
    Views:
    1,526
  5. David Dorward
    Replies:
    1
    Views:
    2,104
    Isofarro
    Jun 28, 2003
Loading...

Share This Page