• 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

Trying to Display an image in Swing

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure why, but every time I try to get a picture to show up in a Java program I am trying to run it doesn't display. Here is basically my code



The image is 223x150. I am using NetBeans IDE and the image is in the src folder (the folder with all the programs). Any ideas?
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's advisable to put your images in an images folder, store your images folder in your classpath and use Class.getResource() to locate your images. For example

 
H Murdock
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the problem is the code. I copied and pasted it to the desktop along with the picture and it worked. Something is up with NetBeans I think. It might also be because I am using subversion. Would any of these cause a problem that you know of? And if so how do I fix it. (I tried that way first and it didn't work either.)
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this helps. This tutorial shows how to add your images to the classpath. I don't think that subversion is the problem because the images should be loaded from the classpath and not SVN.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to stick to your old code, use ImageIcon pc = new ImageIcon("src/file.gif");

or move file.gif from the src folder to the project folder and use ImageIcon pc = new ImageIcon("file.gif");

The problem with this is that your code is reliant on the working directory or an absolute path instead of the classpath. Also, the getResource() method allows you to verify that the image is available so that you can take appropriate action if it isn't.
 
H Murdock
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a problem with changing my code, but nothing seems to work. I have tried several different types of code, using relative paths and specific paths, but it doesn't display the picture. If I got anything to work I would defently try a specific path, but I don't know what the problem is. I also tried to change the icon, but that didn't work either. Do you know how I would fix the problem if it was caused by NetBeans? Thanks for your suggestions so far.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what your main method looks like, but if you use this main method and you save file.gif in the src folder, you will see the image.



 
H Murdock
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All my main does is call the other classes. It doesn't display anything on the screen itself. Is that path with the image in the same folder as all the other files?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find this tutorial helpful.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you posted in a books forum? I shall move this thread to a more appropriate location.
 
H Murdock
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok tank you for your help its working now. I have another problem that I need help with. I am using a progress bar on one of the screens that pop-up. After the user enters a password, a welcome screen is displayed that welcomes the user to the program and shows how much the program has loaded. Instead of actually showing the progress, though, the outer frame is displayed then everything stops as the program loads, then all of the text, image, and progress bar appear. Here is basically my code



I have already checked out the tutorial for this and did not find anything that would help. Adding a button would be useless and a nuisance for the user. Any suggestions?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Concurrency in Swing
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic