wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes trouble displaying ImageIcon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "trouble displaying ImageIcon" Watch "trouble displaying ImageIcon" New topic
Author

trouble displaying ImageIcon

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
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?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Check it all again, works fine for me.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Try this instead, and let me know what happens:

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
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 ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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:

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
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.
Thanks!
---
um... almost there.
the directories the files are in are
C:\Eclipse Workplace\JavaSandbox\bin\com\jj6\ch21
and
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 ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
don't use "." as the directory divider. Use the slash "/" instead.
Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
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.
Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
DB:
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 "//"
 
jQuery in Action, 2nd edition
 
subject: trouble displaying ImageIcon
 
Similar Threads
JLabel icons
Display image in a BorderLayout
Selecting applications using Icons in Swing
How to load an image to display
jpanel paint image using method like jpanel1.paint