This is the best way to handle all images and icons in a JAR application.
Once you have attached all your images and icons to your own JAR file - Customize your build path by adding the JAR image file to the Libraries tab so that it is now included in your class path.
Then simply use the following 3x lines of code at the beginning of your constuctor to access any image you need for anything, including a SystemTray image that does not accept a simple ImageIcon as the main icon (strange, I know). Lines 3x:
URL iconUrl = this.getClass().getResource("/image-iconb.png"); Toolkit tk = this.getToolkit(); imageIcon = tk.getImage(iconUrl);
(imageIcon is just declared by the Image variable constructor) Now you can set the window icon in the same way as:
setIconImage(imageIcon );
and use the same variable when setting System TrayIcon, declaring:
trayIcon = new TrayIcon(imageIcon, "SystemTray Demo", popupMenu);
The above allows you to easily and centrally declare Images or ImageIcons without risking not storing image resources in the right place. This keeps it beautiful and neat: a JAR containing all of your images automatically compiled at run-time and distributed by your program.
As a bonus , once a JAR is registered on your way to the class, you can add any other images to the same JAR at any time at any time without fuss. Everything just works, and the added images are instantly available to your application.
Much better in my opinion.
Martin Sansone - MiOEE
source share