Link to home
Start Free TrialLog in
Avatar of mwalker
mwalkerFlag for United States of America

asked on

Loading images for an applet from a JAR file (Swing)

I'd like to modify the following code so that it will load the images for the applet from the JAR file:

    public static ImageIcon loadImageIcon(Applet applet, String filename)
        throws MalformedURLException
    {
        URL url;
       
        try {
              url = new URL(applet.getCodeBase(), filename);
        } catch(MalformedURLException e) {
            throw (new MalformedURLException());
        }
       
        return new ImageIcon(url);
    }

I have tried using the constructor URL(String filename), giving a relative path to the images which did not work.

Thanks.
Avatar of sailwind
sailwind

using:

new URL(applet.getCodeBase(), filename);

will not create an URL which can bypass the browser's security sandbox.

Convert it to:

url = new URL(applet.getCodeBase() + filename);

and that should work.
Avatar of mwalker

ASKER

I'm sorry, but this didn't work either.  Here's my scenario:

In HTML file:
<APPLET NAME="MyApplet" CODEBASE="../lib" CODE="MyApplet.class" WIDTH="745" HEIGHT="475"
ALIGN="BOTTOM" CODEBASE="myapplet.jar">

All of the images are in a subdirectory of lib.  To test your proposed solution, I moved this directory after adding the images to the JAR file.
ASKER CERTIFIED SOLUTION
Avatar of fontaine
fontaine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial