• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ImageIcon Problem

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. That is completely differ than what I'm doing, obviously. I'll give it a try.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic