CIS 35A: Introduction to Java Programming

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

Applets

Applets
Locating Resource
Creating ImageIcon Using Absolute File Names

You used the ImageIcon class to create an icon from an image file and the setIcon method or the constructor to place the image in a GUI component, such as a button and a label. For example, the following statements create an ImageIcon and set it on an JLabel object jlbl.

ImageIcon imageIcon = new ImageIcon("c:\\book\\image\\us.gif");
jlbl.setIcon(imageIcon);

This approach suffers a problem. The file location is fixed since it uses the absolute file path on Window. Thus, the program cannot run on other platforms and cannot run as applet.

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