Stressful IconImage

I think (and hope) that every developer have been in a situation where a very simple feature just doesn’t work even if everything looks to be okay. This happened to me today when I tried to do a simple thing: I wanted to change the default icon for my JFrame window:

Default JFrame Icon

First I tried the obvious solution. I added an image to my package “images\star_16.png” and then tried to use it in constructor like this:

ImageIcon icon;
icon = new ImageIcon("images\star_16.png");
setIconImage( icon.getImage() );

But this didn’t have any effect on the top left icon. It didn’t throw any exceptions or anything. It just didn’t work.

Then I remembered the Toolkit class that I had used in some of the Java Applets so I changed the code to use Toolkit class like this:

Toolkit tk = Toolkit.getDefaultToolkit();
Image icon = tk.getImage("images/star_16.png");
setIconImage(icon);

This didn’t work either. No exceptions or such.

Then I realized that the image I was trying to load was indeed located inside the JAR file so I should probably use some sort of resource handler:

URL iconUrl = URLClassLoader.getSystemResource(
"images/star_16.png");
ImageIcon icon = new ImageIcon( iconUrl );
setIconImage(icon.getImage());

And voilĂ , it worked!

Custom JFrame Icon

2 Responses to “Stressful IconImage”

  1. satyarajasekhar Says:

    Hi,
    i am developing an application concerning communication between mobile and PC via bluetooth.

    I mean in PC side one application has to be continuously running to accept the connectinos from clients(mobile midlets).

    Please tell me the procedure to do that one.

  2. Tommi Laukkanen Says:

    satyarajasekhar: You could start out by reading my older post on the subject:
    http://www.substanceofcode.com/2007/07/09/using-bluetooth-stack-in-desktop-application/

Leave a Reply