title: How to set icon image for swing application?
body: <p>I've seen many different examples showing how to set a JFrame's <code>IconImage</code> so that the application uses that icon instead of the standard coffee mug.  None of them are working for me.</p>
  
  <p>Here's "my" code (heavily borrowed from other posts and the internet at large):</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>This code fails down in <code>loadIcon</code> when making a call to the <code>getResource()</code> method. To me, there's only 2 possibilities here: (1) the <code>myImage.jpg</code> is in the wrong directory, or (2) <code>getResource()</code> 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 <code>ImageIO</code>.)</p>
  
  <p>I have used the <code>System.out.println(new File(".").getAbsolutePath());</code> 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.</p>
  
  <p>So that leaves me to believe there's something that <code>getResource()</code> 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.</p>
  
  <p>My JPG loads fine in other image viewers, so that's ruled out as well. Anyone have any ideas?</p>
  
  <p>Thanks in advance!</p>