[ale] OT: Swing + Applets

Jason Day jasonday at worldnet.att.net
Thu Aug 28 14:18:44 EDT 2003


On Thu, Aug 28, 2003 at 12:39:50PM -0400, Christopher Fowler wrote:
> Does anyone know if it is possible to run an applet in a 
> Swing or AWT container?

You mean a way to make a hybrid applet/application, that can run as
either?  The simplest way is of course to use appletviewer, but I'm
guessing that's not what you want.

If you want a hybrid applet/application, you will need to add some code
that does what the browser does.  Here is a main method that should get
you started:

public static void main (String args[]) {
    // set applet flag
    // your class should define this variable as a static
    inApplet = false;

    // Create the frame to hold the applet. This can be a
    // Frame or JFrame
    JFrame frame = new JFrame ("Applet Frame");

    // instantiate the applet
    MyApplet applet = new MyApplet();

    // add the applet to the frame
    frame.getContentPane().add (applet, BorderLayout.CENTER);

    // resize the frame and make it visible
    frame.setSize (640, 480);
    frame.setVisible (true);

    // run the methods the browser normally would
    applet.init();
    applet.start();
}

Depending on what your applet does, this might be all you need to do.
If, however, your applet calls getAppletContext(), getParameter(),
getCodeBase(), or getDocumentBase(), then you will need to override
these methods to do something sensible.  That's where the inApplet flag
comes into play: if inApplet is true, you just call the super and you're
done.  There may also be other methods, but I think those are the most
important ones.

HTH,
Jason
-- 
Jason Day                                       jasonday at
http://jasonday.home.att.net                    worldnet dot att dot net
 
"Of course I'm paranoid, everyone is trying to kill me."
    -- Weyoun-6, Star Trek: Deep Space 9
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list