Google

Friday, March 14, 2008

Correct way to add icons to Java Applications for Web Start

The correct way to add icons to a Java Desktop Application that can also load the icon via Java Web Start is described below.


First, add the icon image kcalc.png to the resources folder as shown below:



Then, insert these two lines of code as shown below:



Frame frame = this.getFrame();
frame.setIconImage(frame.getToolkit().getImage(
getClass().getResource("resources/kcalc.png")));


Below is another alternative which is much clearer and much easier to understand:

Frame frame = this.getFrame();
Toolkit mytoolkit = frame.getToolkit();
Class myclass = this.getClass();
String iconpic = "resources/kcalc.png";
frame.setIconImage(mytoolkit.getImage(
myclass.getResource(iconpic)));


This way, the icon image kcalc.png will be packed as a single jar along with the main class and will be correctly delivered together via Java WebStart.