CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Applets

Applets
Locating Resource
Creating ImageIcon Using Relative File Names

Assume that image/us.gif is under the class directory, you can circumvent this problem by using a relative path as follows:

ImageIcon imageIcon = new ImageIcon("image/us.gif");
jlbl.setIcon(imageIcon);

This works fine with Java applications on all platforms, but does not work with Java applets because applets cannot load local files. To make it to work with both applications and applets, you need to locate the file using the URL class.

Previous | Locating Resource from Applets | Creating ImageIcon Using Absolute File Names | Creating ImageIcon Using Relative File Names | Using the URL Class | Creating a URL from a Class Reference | Displaying image