aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ImageIcon Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ImageIcon Problem" Watch "ImageIcon Problem" New topic
Author

ImageIcon Problem

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I'm trying to create a JLabel that contains an image but it will not display. From examples I found, here is what I came up with for the JLabel definition.

Since I do not get the text "KCBS Logo" I must be getting a non-null for logoURL. When I print imageIcon description I get:

file:/H:/html/Applets/KCBSEvents/bin/KCBSEvents/img/KCBSLogo.jpg

If this is supposed to be a true URL then it is invalid and should be:

file:///H:/html/Applets/KCBSEvents/bin/KCBSEvents/img/KCBSLogo.jpg

The path is correct and I can plug the second URL into my browser and see the image. The first produces an error in the browser. So what am I doing wrong? TIA.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks for the reply. That is completely differ than what I'm doing, obviously. I'll give it a try.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Ran into one small problem. What is getImage? I can't seem to resolve it.

Is this part of the Applet class? What if I am not creating an applet?
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I answered my question myself by trying it. While building a standalone program it apparently doesn't matter if I use a JApplet class for this issue. Anyway, it didn't help, as I still have the same problem. However, it did prove what I already knew, it cannot find the file. I am guessing that the crux of the problem is that I am not specifying the path correctly. The 'img' sub-directory is in the same directory as the .java files containing all the classes. I even tried moving the image file to the same directory and referencing it without the 'img/' to no avail. Where is the darn thing supposed to be relative to the class definition? TIA.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I just tried my posted code, changing
new java.net.URL(getCodeBase(),"test.gif")
to
new java.net.URL(getCodeBase(),"img/test.gif")

and the image displayed fine.

One of the problems I have found with applets is the way they cache the class files.
compile the class, find something's not right, make a change, recompile/rerun, and the
original class file is used.

Create another folder, put the .java, .html and .jpg files there.
Copy the code I posted into the .java file (changing the name of the image file),
then compile and run the html file.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks but I found using your Applet method opens a default Applet window which I don't want. I couldn't get that to work either in any case. It kept throwing an InstantiationException. The main problem at this point is that I don't know how to generate a useful error message explaining why the file is not found or loaded. I even tried an absolute path as suggested in another example. That example was quite simple and had me do this:

There is no exception thrown so something legitimate is being put into 'logo' but whatever it is, is not being displayed in the JLabel.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Thanks but I found using your Applet method opens a default Applet window which I don't want.

I may have led you astray. In your first post I saw this
file:/H:/html/Applets/.....

and assumed you were trying to display an image in an applet, so I posted the applet example with getCodeBase()

Probably best now to post a compilable/runnable example, so we can run it and see if it works for us (or not).
not a lot of code, just enough to display an image in a JLabel in a JFrame or whatever container you're using
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks for the help. I started out writing a couple of simple applets and then started on this standalone program. I didn't bother to change the directory and just started a new project in the same one. Thats why it wound up with that directory in the path.

Anyway, I'm trying to produce a splash screen and the class definition is not that big so this should give you enough.

I'm assuming you can use any jpg and don't need the one I have.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
after adding the imports and a main(), also removing all references to the class CountDownLatch,
the code compiled, ran and displayed my images fine (.gif and .jpg).
The images were in the same folder as the .java and .class files

also, there is a java.awt.SplashScreen class which might make it easier for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ImageIcon Problem