mwalker
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(MalformedURLExceptio n 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.
public static ImageIcon loadImageIcon(Applet applet, String filename)
throws MalformedURLException
{
URL url;
try {
url = new URL(applet.getCodeBase(), filename);
} catch(MalformedURLExceptio
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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.