my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Trying to Display an image in Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Trying to Display an image in Swing" Watch "Trying to Display an image in Swing" New topic
Author

Trying to Display an image in Swing

H Murdock
Greenhorn

Joined: Mar 23, 2011
Posts: 10
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?
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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

Joined: Mar 23, 2011
Posts: 10
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

Joined: May 13, 2002
Posts: 180
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

Joined: May 13, 2002
Posts: 180
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

Joined: Mar 23, 2011
Posts: 10
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

Joined: May 13, 2002
Posts: 180
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

Joined: Mar 23, 2011
Posts: 10
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?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

You might find this tutorial helpful.


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

Joined: Oct 13, 2005
Posts: 38087
    
  22
Why have you posted in a books forum? I shall move this thread to a more appropriate location.
H Murdock
Greenhorn

Joined: Mar 23, 2011
Posts: 10
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

Joined: May 03, 2008
Posts: 4523
    
    5

Concurrency in Swing
 
jQuery in Action, 2nd edition
 
subject: Trying to Display an image in Swing
 
Similar Threads
Unable to set label icon
to add image in prog
ImageIcon not showing Ubuntu
Images don't appear in Swing code
Adding an Image to JPanel using NetBeans IDE