Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble displaying ImageIcon

 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1223
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check it all again, works fine for me.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this instead, and let me know what happens:

 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't use "." as the directory divider. Use the slash "/" instead.
 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 "//"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic