2

I've seen many different examples showing how to set a JFrame's IconImage so that the application uses that icon instead of the standard coffee mug. None of them are working for me.

Here's "my" code (heavily borrowed from other posts and the internet at large):

public class MyApp extends JFrame
{
    public MyApp()
    {
        ImageIcon myAppImage = loadIcon("myimage.jpg");
        if(myAppImage != null)
            setIconImage(myAppImage.getImage());
    }

    private ImageIcon loadIcon(String strPath)
    {
        URL imgURL = getResource(strPath);
        if(imgURL != null)
            return new ImageIcon(imgURL);
        else
            return null;
    }
}

This code fails down in loadIcon when making a call to the getResource() method. To me, there's only 2 possibilities here: (1) the myImage.jpg is in the wrong directory, or (2) getResource() doesn't like something about my image (I had to convert it from CMYK to RGB in Photoshop so I could use the same image elsewhere with ImageIO.)

I have used the System.out.println(new File(".").getAbsolutePath()); trick to locate the directory where the image JPG should be stored, and still nothing worked. I have subsequently placed the JPG in just about every directory inside my project, just to rule file location out as the culprit.

So that leaves me to believe there's something that getResource() doesn't like about the JPG itself. But I have now already exhausted my understanding of images and icons in the mighty, wide world of Swing.

My JPG loads fine in other image viewers, so that's ruled out as well. Anyone have any ideas?

Thanks in advance!

progyammer
  • 1,498
  • 3
  • 17
  • 29
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • To check if the problem is with the URL try: `System.out.println("estimated number of bytes in the file = " + imgURL.openStream().available());`. – toto2 Aug 25 '11 at 18:36
  • Thanks toto2 - now we're finally getting somewhere! That line throws an exception with a message of "null"... easily the worst and least helpful exception message I've ever seen. Any clues? – IAmYourFaja Aug 25 '11 at 18:47
  • `available` is not a rock solid method according to the api, so we can't conclude much. You could try to split `openStream().avalaible()` over two different lines to see where the error is from. My guess is that your image is fine, but cannot be located. Since you are using NetBeans, you can also take a look at this [tutorial](http://netbeans.org/kb/docs/java/gui-image-display.html). – toto2 Aug 25 '11 at 19:03
  • 2
    @Mar, Why does nothing work for you? Why didn't the suggestions in this posting (http://stackoverflow.com/questions/7181699/changing-swing-jtable-cell-colors) work for you when they work for everybody else?? Why can't you post a SSCCE like you've been asked to do before??? We are NOT mind readers, we can't guess why it works for everybody else and not for you!!! – camickr Aug 25 '11 at 19:07
  • Camickr please refrain from posting answers/responses to my questions in the future. – IAmYourFaja Aug 25 '11 at 19:12
  • 2
    Sorry, I take offence when people continue to post questions without all the necessary information needed to solve the problem. You continue waste the time of everybody who takes the time to read this question. Not only that, you have the nerve to send me a direct email from my blog asking for help, when you don't want me to help your here! – camickr Aug 25 '11 at 19:27

5 Answers5

3

I have tried following which was a answer for same kind of question like yours. And it works for me.

Try putting your images in a separate folder outside of your src folder. Then, use ImageIO to load your images. (answered Aug 27 '13 at 0:18 AndyTechGuy)

frame.setIconImage(ImageIO.read(new File("res/icon.png")));
Community
  • 1
  • 1
Vidu
  • 421
  • 5
  • 6
2

put the image in the root of the classpath and say getResource("classpath:myimage.jpg");

The problem with your code is that jvm is unsure where to lookup the image file so its returning null.

Here is a nice link about classpath

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
  • I understand the classpath to be the directory where the JRE will look for .class files. If that's correct then my NetBeans automatically creates a fairly complicated build/ directory that has a folder called classes/, which itself contains a my top-level com/ package, META-INF/ and res/. I have placed the image in every single one of these directories and prefixed the "classpath:" string in the getResource call like you specified. Still no go... I am still thinking it doesn't like the JPG – IAmYourFaja Aug 25 '11 at 18:11
1

It should be

if(imgURL != null)
           ^

instead of

if(imgURL !- null)

and

URL imgURL = this.getClass().getResource(strPath);

instead of

URL imgURL = getResource(strPath);

Then it works fine, if "myimage.jpg" is in the same dir with MyApp.class

oliholz
  • 7,447
  • 2
  • 43
  • 82
  • Thanks - but the first one (imgURL !- null) was obviously a typo as that would never compile. Made the 2nd change like you recommended, and made sure the image is in the same folder as MyApp.class. Still no go... – IAmYourFaja Aug 25 '11 at 18:15
0

You can try to use a "/" before your filename.

getClass().getResource("/myimage.jpg")

If you look into your build-output folder (target) you can look for your class where you are trying to get your resource from. Your resources will probably be copied in some folders above.

For example your target directory could look like this:

target
 |- de.example.app
     |- Main.class
 |- Main-x.y.z.jar
 |- myimage.jpg

So if you just go for

getClass().getResource("myimage.jpg")
it will look under the folder target/de/example/app and won't find a jpg there.
You need to tell him that you want to look under the root-folder (target/**). That's why you need to place a "/" before your file.
Xinho
  • 21
  • 6
0

Two suggestions:

  1. Try using the getClass().getResource("x.jpg"), and putting the file directly in the same folder as the .class file of the class you are in.
  2. Make sure the name is identical in case - some operating systems are case sensitive, and within a JAR, everything is case sensitive.
Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58