Search code examples
jframeimageicon

How to change the image of my java app icon


I have searched for an answer to this question in many places, but none of the answers satisfies what I really want. There are variations of the solution, but they all go along the lines of the code I have below:

MainFrame() {
   ImageIcon logo = new ImageIcon(getClass().getResource("file_path"));
   setIconImage(logo.getImage);
   //the rest of my code
}

public static void main(String[] args){
   MainFrame mf = new MainFrame();
   mf.pack();
   mf.setVisible(true);
}

Basically, load an image or an image icon from a file, and then call the setIconImage() method from the JFrame. This sets the icon image in the title bar of the window, and also the icon image in the task bar when the app is active. But it does not set the icon image of the app icon. In order to show you more clearly what I mean, please see the image I attached, where I circled the three icons. The one with the Java coffee cup is the one I haven't been able to change.

the taskbar, my app, and the window of my app

Can somebody help me set the image of the app icon itself (not just the task bar and title bar icons)?


Solution

  • I was not able change the image of the jar file itself as it sits inside a directory, as shown in the image attached to my question. However, I was able was to set the icon image of the .exe file when I packaged it with jpackage.

    Basically, once I was ready to package my app I entered in Command Prompt, making sure to include the --icon option:

    jpackage --input C:\app_files\MyApp --name MyApp --main-jar myapp.jar --main-class main.MainThread --icon C:\app_files\MyApp\logo.ico --win-shortcut --dest C:\Users\Desktop\Apps --runtime-image C:\app_files\my_jre
    

    Note: Here, at least for Windows 10, the icon image must be an .ico file.