3

I have written an SWT Java application and would like to configure it to use a high quality icon in the OSX dock. This is my current code:

// Prepare window
final Shell window = new Shell();

// Load icons
Display display = window.getDisplay();
ClassLoader loader = InTraceStandaloneUI.class.getClassLoader();
InputStream is16 = loader.getResourceAsStream(
                 "org/intrace/icons/intrace16.gif");
Image icon16 = new Image(display, is16);
is16.close();
InputStream is32 = loader.getResourceAsStream(
                 "org/intrace/icons/intrace32.gif");
Image icon32 = new Image(display, is32);
is32.close();
window.setImages(new Image[] {icon16, icon32});

https://github.com/mchr3k/org.intrace/blob/master/org.intrace/src/org/intrace/client/gui/InTraceStandaloneUI.java

This works for loading the 16x16 and 32x32 logos which look OK on Windows but the logo used by OSX still looks really pixelated. Do I just need to specify a third version with a higher resolution or should I be using a different image format or API?

mchr
  • 6,161
  • 6
  • 52
  • 74

3 Answers3

5

Actually, if you call Shell.setImages(...) and include a 128x128 icon then this will be chosen by OSX and you get a high quality dock icon without having to use the Apple extension classes.

mchr
  • 6,161
  • 6
  • 52
  • 74
3

You can detect OSX and use the Apple Java Extensions and call Application.setDockIconImage(...)

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • What resolution and image file format should I use with this API? I have seen both 128x128 and 256x256 discussed but I'm not sure whether specifying 256x256 will always work? – mchr Jun 06 '11 at 16:11
  • I don't know what the highest resolution supported nowadays is. I would try with 128x128 first... – Dilum Ranatunga Jun 06 '11 at 16:15
  • https://github.com/mchr3k/org.intrace/blob/master/org.intrace/src/org/intrace/client/gui/InTraceStandaloneUI.java - My class now loads a high res logo. The low res icon is used for a few seconds but I don't care enough to work out why. – mchr Jun 09 '11 at 08:32
  • I imagine it is during startup, where the JVM has started, but the call to setDockIconImage(...) has not yet been reached. If this answered your question, then please accept. – Dilum Ranatunga Jun 09 '11 at 11:37
  • As you can see in the linked source code I am calling setDockIconImage before anything else and BEFORE I set the low res icons on the window. Any idea why the low res icon gets shown before the high res icon which was set first? – mchr Jun 09 '11 at 13:32
  • 1
    My guess would be that setDockIconImage(...) doesn't actually do anything until the UI is up. I know that the best way to get around all of this is to package your Java application as a OSX application. See this conversation: http://stackoverflow.com/questions/2695214/convert-jar-to-an-osx-executable – Dilum Ranatunga Jun 09 '11 at 13:49
1

Shouldn't the dock icon be referenced in the Info.plist using the

<key>CFBundleIconFile</key>
<string>icon.icns</string>

lines?

Mot
  • 28,248
  • 23
  • 84
  • 121
  • You are correct but I was trying to get the best possible experience from a jar which is launched directly without being packaged with an info.plist. – mchr Jul 14 '11 at 11:34