Controlling Parent Applet from Opened Application

I have opened an application from an applet, that provides a list of choices and when the user selects one and clicks the appropriate button, I need to jump to the URL associated with the choice.  I had it working in the application but Applets evidently open web pages with

getAppletContext().showDocument("www.mysite.com");

or something similar.  How would I have the application tell the applet to open that site?

Thanks as always!
mh149Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
heyhey_Connect With a Mentor Commented:
make a public static variable inside your applet class

public class MyApplet extends Applet
{
public static MyApplet theApplet;
public void inti()
{
  theApplet = this;
  ....
}
....
}

now you can use theApplet.getAppletContext().showDocument()

1. note that there are several other solutions (your code just needs a reference to the applet and there are a lot of ways to to set this reference).
2. note that this code won't work very well when user has loaded more than one instance of your applet.
0
 
mh149Author Commented:
Here is my loader applet class:

import java.applet.*;
import java.awt.*;

public class LoaderApplet extends Applet
{
      public static LoaderApplet parentApplet;
      
      public void init()
      {      
            parentApplet = this;
            Search Search_App = new Search();
      }
}


Now in my Search Class, when I try and access parentApplet, I get this

import org.gjt.mm.mysql.Driver.*;
import org.gjt.mm.mysql.Connection.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
import java.net.URL;

public class Search extends Frame implements MouseListener, WindowListener, ActionListener, ItemListener
{      
     .
     .
     .
private void goto_URL(String strURL)
      {
      parentApplet.getAppletContext().showDocument (User_URL);
      }
     .
     .
     .
}

It gives me the error:
   Undefined name 'parentApplet'
and when I run the the applet I get:

java.lang.NoSuchMethodError: Search: method <init>()V not found
      at LoaderApplet.init (LoaderApplet.java:11)
      at com/ms/applet/AppletPanel.securedCall0 (AppletPanel.java)
      at com/ms/applet/AppletPanel.securedCall (AppletPanel.java)
      at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
      at com/ms/applet/AppletPanel.run (AppletPanel.java)
      at java/lang/Thread.run (Thread.java)


Am I supposed to link to the LoaderApplet class somehow?

0
 
heyhey_Commented:
replace this
parentApplet.getAppletContext().showDocument (User_URL);

with
LoaderApplet.parentApplet.getAppletContext().showDocument (User_URL);

parentApplet is public static field of LoaderApplet

so you can use it from everywhere with
LoaderApplet.parentApplet

(and you've already initialized it to point to the current applet instance)
0
 
mh149Author Commented:
You are awesome - thanks a lot.
0
All Courses

From novice to tech pro — start learning today.