Forums Register Login
trouble displaying ImageIcon
I have a piece of code from a book I'm working with. I haven't adjusted the code at all and so am surprised that it isn't working properly out of the box.

the "star.gif" file is sitting in the same folder as the source code being compiled, and in the bytecode being run. The app runs but it just displays the centered string, and no image. Is there any reason that is obvious in this code as to why this isn't displaying?
Check it all again, works fine for me.
Try this instead, and let me know what happens:

Pete Stein:
that's not working for me either. I've noticed that some of the other classes in this chapter's code also use this method to display ImageIcons and none of the GUIs their creating are displaying it for me. I'm working with eclipse and can see the images sitting in the same package as the source code. I can also confirm this in a Windows Explorer window, but (i'm guessing) more importantly I can confirm that the images are also in the bin. (I'm going to venture an educated guess that eclipse copies images to the bin at compile, because I didn't put those there.)
So something is definitely going wrong that is not involved with whether or not the source code being written properly. Which upsets me.
[ June 22, 2008: Message edited by: Tristan Rouse ]
Try compiling and running this program. It will tell you where Java is looking for your file. Once you know this, you can add the necessary subdirectories to your file path string to make it work:

Gotcha, I figured that java would look in the same place the class files were located. Its actually pointing to the root of the eclipse project space i'm in.
I'll just add the rest of the directory information when I need to use a path for an Icon like that.
um... almost there.
the directories the files are in are
C:\Eclipse Workplace\JavaSandbox\bin\com\jj6\ch21
C:\Eclipse Workplace\JavaSandbox\src\com\jj6\ch21

Java wants me to use .'s instead of /'s or \'s int the String I pass to the ImageIcon constructor so it can be extra multi-platform friendly right?

Am I doing that wrong? It still won't display the image.
[ June 22, 2008: Message edited by: Tristan Rouse ]
don't use "." as the directory divider. Use the slash "/" instead.
right, I guess the jvm hands that string over to native. I actually had to use "//" but I knew what you meant and got it working. thanks for all your help.

Originally posted by Tristan Rouse:
right, I guess the jvm hands that string over to native. I actually had to use "//" but I knew what you meant and got it working. thanks for all your help.

No way. You might have to use "\\" if you use a backslash as the path separator, but never "//".

The forward slash is a system independent path separator.
Probably better to use File.pathSeparator which makes for more portable code. The same attribute is available via the System#getProperty and System#getProperties methods.
Odd, You're right that it works fine with "/". Obviously I got my slashes backword (or forward...) now I'm just surprised that it worked with "//"
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 4403 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 15, 2018 20:57:24.