How to Change the Dock Icon of a Java Program

How do you change the Dock Icon of a Java program?

Apple eAWT provides the Application class that allows to change the dock icon of an application.

import com.apple.eawt.Application;
...
Application application = Application.getApplication();
Image image = Toolkit.getDefaultToolkit().getImage("icon.png");
application.setDockIconImage(image);

Changing the default java coffee dock icon to something else

So, I did a really quick test using...

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import com.apple.eawt.Application;

public class Test {

public static void main(String[] args) {
try {
Application application = Application.getApplication();
application.setDockIconImage(ImageIO.read(Test.class.getResource("/javaapplication163/bunny.jpg")));
} catch (IOException ex) {
ex.printStackTrace();
}
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

public TestPane() {
add(new JLabel("Hello"));
}

}

}

And it showed...

Hello Bunny

Neat ... however, this will only compile and run on a Mac.

A "typical" solution to this would be to use reflection to try and load the com.apple.eawt.Application at run time, something like...

try {
Class appClass = Class.forName("com.apple.eawt.Application");
Class params[] = new Class[]{Image.class};

Method getApplication = appClass.getMethod("getApplication");
Object application = getApplication.invoke(appClass);
Method setDockIconImage = appClass.getMethod("setDockIconImage", params);
setDockIconImage.invoke(application, ImageIO.read(Test.class.getResource("/javaapplication163/bunny.jpg")));

} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException | IOException ex) {
ex.printStackTrace();
}

But this is some what error prone and messy, not to mention a complete pain in the ... code to maintain.

So, then I thought I might try using the command line parameters, as demonstrated at The dock icon...

-Xdock:name="Hello bunny" -Xdock:icon=/path/to/bunny.jpg

which also worked.

While I kind of prefer this solution, it does mean that the icon needs to be externalised from Jar, which is kind of a pain, and you'll probably need to supply a full working path.

If I was to generate a App bundle for my project, I'd probably follow this, but if I was just deploying a Jar, I might be tempted to use one of the previous methods

How do you set the Dock Icon of a Java program?

Yes. The class you seek is part of a Maven artifact, namely com.apple.AppleJavaExtensions. Either use Maven in your build, or just download the JAR of the artifact and add it to your build path--this artifact itself has no dependencies.

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

setIconImage does not set the jar icon. It will set the icon for what the minimized window for that JFrame will look like. The jar icon (which controls the finder icon and the dock application icon) cannot be set in the jar file itself. You just get the default icon provided by the OS. You will need to wrap it using something like JarBundler for OS X or Launch4J for Windows.

You can set the application dock icon while your application is running, see com.apple.eawt.Application.setDockIconImage. It isn't perfect though, because when you double-click on your jar, it starts up in the dock using the generic java icon and only switches to your custom icon after a bounce or two when the java code starts running. Also, I don't think it would set the dock icon for an jar which isn't running (not that you can drag a jar file into the dock anyway - doesn't seem to work for me).

Here's some code that demonstrates the different images you can set:

import com.apple.eawt.Application;
import javax.swing.*;

class SetIcon extends JFrame {

SetIcon() {
setIconImage(new ImageIcon("doc.png").getImage());
Application.getApplication().setDockIconImage(
new ImageIcon("app.png").getImage());
}

public static void main(String args[]) {
SetIcon s = new SetIcon();
s.setVisible(true);
}
}

Dock icon under OSX in Java 9

Have you tried setIconImage in the new java.awt.Taskbar class?

I want to make my java program have an icon in the Taskbar for Windows and the Dock for Mac

For Windows, start by taking a look at Window#setIconImages, demonstrated here

For MacOS, it becomes a little more difficult. You need to make use of a custom library (available only on the Mac)

Apple eAWT provides the Application class that allows to change the dock icon of an application.

import com.apple.eawt.Application;
...
Application application = Application.getApplication();
Image image = ...
application.setDockIconImage(image);

Also demonstrated here

As stated in the example linked above, the preferred method for depleying on MacOS would be to make use of the Mac OS application bundler

Changing Eclipse Dock Icon

Eclipse allows the redefinition of Dock icons using different products. The various packages of Eclipse provide such products, e.g. there is a different icon for Java EE package.

I think, but not entirely sure, that you are experiencing such an icon change. To resolve it, you have to manually edit the eclipse.ini file next to eclipse.exe - make sure you make a backup before editing!

Look for a line similar to the following:

-product
org.eclipse.epp.package.modeling.product

The org.eclipse.epp.package.modeling.product is an Eclipse product id that is launched and that provides the icon, splash screen, etc. If you replace that line with a different product, you can get back the original icon after a restart.

How to determine the correct product ID:

  1. If it begins with org.eclipse.epp it is a deployed edition downloaded from eclipse.org - in this case, it probably wasn't that the issue (unless you are complaining about the Java EE packages specific icon).
  2. With no plug-in development knowledge: download a new copy of Eclipse, and look for its eclipse.ini file. You don't have to start it, just copy the correct line.
  3. If you have the Plug-in development environment installed, and you are using it, then simply open a Run configuration, and on the Main tab look for the Product list dropdown - the listed ones are all valid and installed products.
  4. Hard-core solution: you can ask the p2 director from the command line of the installed products. Unless you are experienced with this tool, don't try to use this solution (step 2 or 3 is much simpler).

Change Java application icon?

import javax.swing.JFrame;
import javax.swing.ImageIcon;

class JFrameTest
{
public static void main(String _[])
{
JFrame jFrame = new JFrame("Hello World!!");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setIconImage(new ImageIcon("c:/home/ravi/creampink.png").getImage());
jFrame.setSize(400,400);
jFrame.setVisible(true);
}
}

Just search for Download 128X128 icon in google to get some sample icons



Related Topics



Leave a reply



Submit