views:

147

answers:

1

I'm trying to display two pictures on my JFrame, the way I found was to use icons and JLabels, this seems pretty straightforward and I'm not having problems with this. But when it comes to locating the image I can't get it to work. I'm on a linux machine thus the forwardslash style. I created a folder called pics in my project which is called 399assig1.

        ImageIcon icon1 = createImageIcon("/home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg","First");
    this.label1 = new JLabel("Picture 1", icon1, JLabel.CENTER);

    ImageIcon icon2 = createImageIcon("pics/fur.png","Second");
    this.label2 = new JLabel("Picture 2", icon2, JLabel.CENTER);

this is the error I get

Couldn't find file: /home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg
Couldn't find file: pics/fur.png
+5  A: 

if createImageIcon() is searching the CLASSPATH for the file, you'd need to add the root directory to the CLASSPATH. A better approach would be use a path that is relative to a directory that is already included in the CLASSPATH.

Like so:

%>CLASSPATH=$CLASSPATH;/home/dsk03/ugrad/jeanbern/workspace  

then your call would be

ImageIcon icon1 = createImageIcon("399assig1/pics/fur-05.jpg", "MyIcon");
Kelly French