Setting the default application icon image in Java swing on OS X

前端 未结 5 2134
说谎
说谎 2020-12-06 02:10

I\'m trying to set the icon image for a Jar file:

setIconImage(new ImageIcon(getClass().getResource(\"logo.png\")).getImage());

When runnin

5条回答
  •  攒了一身酷
    2020-12-06 02:39

    Just to add my final solution for adding a MacOSX Dock icon in a pure-java application:

    public boolean exists(String className)
    {
        try {
            Class.forName( className, false, null );
            return true;
        }
        catch (ClassNotFoundException exception) {
            return false;
        }
    }
    
    public void setIcon( BufferedImage icn )
    {
        if ( exists( "com.apple.eawt.Application" ) )
        {
            com.apple.eawt.Application.getApplication().setDockIconImage( icn );
        }
    }
    

    This silently ensures the class is available and the executes the setDockIconImage() method. Works very well for me and it does not interfere with other platforms.

提交回复
热议问题