Changing Application Dock Icon javaFX programatically


Question

I have a javaFX Application that will only load from a JAR however I wanted to change the icon in the mac dock. I have managed to working it out on windows. I am using Netbeans IDE, and would prefer to not add the additional apple JAR Extension file. It is because of this I’m not sure if it is possible.

1
3
5/10/2017 3:33:49 PM

So far I haven't seen a way to do this with JavaFX but there is a way to do it with Apple-specific Java APIs:

public static void main(String[] args) {
    try {
        URL iconURL = Main.class.getResource("ui/resources/Logo@2x.png");
        Image image = new ImageIcon(iconURL).getImage();
        com.apple.eawt.Application.getApplication().setDockIconImage(image);
    } catch (Exception e) {
        // Won't work on Windows or Linux.
    }

    launch(args);
}

This works at least with Oracle Java 1.7.0_40 and 1.8.0_25.

The downside is that when you start the JAR, you'll see the Java logo in the dock for a short time which then changes to your icon. This is a technical limitation and can only be worked around by creating a real, native OS X bundle.

9
12/19/2014 2:04:33 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon