Wednesday, 17 March 2010

How To Change the Default Icon With The Swing Java Application Framework

Since I have been out of work I have been usefully relearning my old Java skills and catching up on some of its new features.

Anyway this morning I have been struggling with how to change the default icon for the application from the boring old Coffee Cup.

In normal applications the way to do this is simply
setIconImage( Toolkit.getDefaultToolkit.getImage("full image file name"));
in your JFrame constructor.

With the java Swing application framework you have to..
  1. Copy the image into the resources directory.
  2. Find the properties file for your main View class.
  3. Add a key, something like iconImage=my short filename (i.e. just the filename without the path.)
  4. Then add the following code to the start of the constructor for your view..
    Image img = (Image)getResourceMap().getObject("iconImage",Image.class);
    getFrame().setIconImage(img);
Anyway, it took me an hour or so to work that out this morning, and many of the answers on the Java forums seemed nothing short of useless. I hope therefore, that this post comes in handy to all those budding Java programmers out there.

No comments: