This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes ImageIcon trouble 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 "ImageIcon trouble" Watch "ImageIcon trouble" New topic

ImageIcon trouble

Josh Ziegler

Joined: Mar 30, 2004
Posts: 4
I'm doing a quicky project for my high-school Programming class and so far i'm having trouble loading a image and returning it as a ImageIcon I need the image to be a Icon so I can use it on a button. Any help is appreciated. BTW this code was taken off this website to speed up development so....
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Read me..
It should be very easy to replace the JLabel in the example given with a JButton using a bit of thought. Notice that;" target="_blank" rel="nofollow"> returns an Image not an ImageIcon - using this image an ImageIcon can be instantiated and used to construct a JButton.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Your method has a return type of ImageIcon so java is expecting an ImageIcon in the return statement at the bottom of the method. But you are returning an image. The compiler will complain about this. The return type declared in the method signature must match the type returned by the method.

Your class will also need a main method. ImageIO is new in JRE1.4.
[ March 30, 2004: Message edited by: Craig Wood ]
Enrique Fernandez

Joined: Mar 29, 2004
Posts: 2
If you are planning to load your image from a jpg or gif source here's a little example using a JDialog to display a JButton with the image:
public class ImageLoad extends JDialog{
private Image imagen;
public ImageLoad (Frame padre, String titulo, boolean modal) throws Exception {
super(padre, titulo,modal);
public void dibujaLog() {
imagen = Toolkit.getDefaultToolkit().getImage("your.jpg");
ImageIcon aceptIcon = new ImageIcon(imagen);
JButton jb = new JButton(aceptIcon);
public static void main(String args[]) throws Exception{
ImageLoad log = new ImageLoad(null, "Access", true);
Josh Ziegler

Joined: Mar 30, 2004
Posts: 4
Thanks for the help first off.
Craig -> Why do I need a main when I'm using the class to support my main project? Is it really neccessary? I'm going to be calling this classes loadImage method (or whatever its called) lots and it may be different every time (at least the file will be).
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Suggestion: change the line:
URL url = ShowingAnImage.class.getResource(fileName);
URL url = ShowingAnImage.class.getClassLoader().getSystemResource(fileName);

Then regardless of what package ShowingAnImage belongs to, the file will be found. (It will still work without a package being specified)
For instance

The directory structure is
Then execute java test.ShowingAnImage and the image file is found as we expect.
[ March 31, 2004: Message edited by: Carlos Failde ]
Eddie Felson

Joined: Mar 31, 2004
Posts: 1
I was really interested in this thread as I was having problems myself getting Images to display. I have tried this code out and it really worked for me. Which is great as I am a novice coder, just into my second year at University.
However the example deals with just one image and I am struggling to make the image loader class to be a generic class that would deal with any image I choose to display in my project.
Is this easy to do?
Are there any default generic imageLoad functions I could use?
Many thanks in advance, your code was really neat.
Any help you can give at all would be very much appreciated.
Consider Paul's rocket mass heater.
subject: ImageIcon trouble
Similar Threads
How to load an image to display
jar file help please
How to display a image in a Frame
ImageIcon problem