Changing Desktop Icon for Java Application

Jeremy Johnson picture Jeremy Johnson · Aug 11, 2013 · Viewed 15.3k times · Source

I wish to thank you in advance for taking the time to read my question, and I would greatly appreciate any comments, answers, insights, techniques and critiques that you may be able to provide.

I'm looking for a useful method for changing the desktop icon for a Java application. I've looked into this for a few days now, but am not finding an accurate result.

Before you mark this down and call it a duplicate, I have read: How do I change the default application icon in Java? to others who asked this question), but this does not address my specific problem. I know that their method utilizes a url location instead of an import, but I am trying to learn how to use this with the import(if that is, in fact, possible). When I attempt to use their method for changing by source location. Besides that, the url example doesn't seem to work for a file stored on the computer. I get an "uncaught error" message when I attempt to run it.

I use the following format to declare an image that I have imported into NetBeans:

Image image = new ImageIcon("imported.png").getImage();
frame.setIconImage(image);

Now this works fine for the icon that displays in the toolbar and it also appears in the upper left-hand corner of the frame, but I still have the Java coffee-cup as the icon for the application when I clean and build it.

For additional resources to the code that I am using to attempt this:

import java.awt.Image;
import javax.swing.*;

public class Check {
    JFrame frame;
    public static void main(String[] args) {
        new Check().go();
    }
    private void go() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Image image = new ImageIcon("owl.gif").getImage();

        frame.setIconImage(image);
        frame.setVisible(true);
        frame.setSize(300, 300);
    }
}

The "owl.gif" bit is what I imported into NetBeans by click and drag method (as described in one of the books that I read that focused on NetBeans).

I'm looking for a way to make a file that I already have saved on my computer the desktop icon for my application after it is built.

Answer

Andrew Thompson picture Andrew Thompson · Aug 11, 2013

For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start1. JWS works on Windows, OS X & *nix.

  1. JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

The 'desktop integration' will use the image identified in the launch file as the desktop or menu item icon.