Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Application Dock Icon javaFX programmatically

Tags:

macos

javafx

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.

like image 290
Sophia_xoox Avatar asked Jun 11 '14 09:06

Sophia_xoox


1 Answers

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/[email protected]");
        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.

like image 66
DarkDust Avatar answered Oct 16 '22 00:10

DarkDust