0

for a simple Java Desktop Application I added a JFrame Form with the assistant of the NetBeans IDE. For this frame I want to change the icon in the title bar. I tried to do so with the following code at the very end of the constructor in the generated View class:

ImageIcon ii = new ImageIcon(iconUrl);
    this.getFrame().setIconImage(ii.getImage());

The String iconUrl is definitely correct, the object ii seems to be alright as far as I can judge from the variables overview in the debugger perspective. However, the icon in the title bar does not change, it's still the default java icon.

Why?

bogus
  • 867
  • 6
  • 14
  • 24

3 Answers3

5

You can try this:

Image i = ImageIO.read(getClass().getResource("/path/to/image"));
setIconImage(i);

Note that here / would represent your src directory

Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43
0

Try this code

Image i = new ImageIcon(ClassLoader.getSystemResource("signal/icm/gui/images/oconp.png")).getImage();
setIconImage(i);
Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31
  • Never use ClassLoader for accessing Non System Resources, always use getClass().getResource(stringPath); Watch for the line in this [Access to Resources](http://docs.oracle.com/javase/7/docs/technotes/guides/lang/resources.html) document, "All class loaders will search for a resource first as a system resource, in a manner analogous to searcing for class files.". – nIcE cOw Feb 17 '12 at 12:24
0

Hope following solution works for you:

ImageIcon icon = new ImageIcon(IconURL);  
myImg = ImageIcon.getImage();   
JFrame.setIconImage(myImg);
limonik
  • 499
  • 1
  • 6
  • 28
me_digvijay
  • 5,374
  • 9
  • 46
  • 83