views:

1231

answers:

1

I'm developing a desktop application using Java. I want to put an icon (with a contextual menu) on the system tray (called Menu Extras in Mac Os). Java 6 comes with support for doing this in Windows and Linux, but it doesn't work in Mac Os.

I have seen some applications doing what I want in all three operating systems (e.g. DropBox), but I don't know if they are made with Java.

How can I achieve this?

If it's not possible in Java, is there any other cross-platform language able to do that?

Thanks.

+4  A: 

AWT / Swing

According to documentation, OSX 10.5 update 1 and newer support TrayIcons

TrayIcons are represented on Mac OS X using NSStatusMenus that are presented to the left of the standard system menu extras. The java.awt.Image artwork for a TrayIcon is presented in grayscale as per the Mac OS X standard for menu extras.

TrayIcon.displayMessage() presents a small non-modal dialog positioned under the TrayIcon. The ActionListener for the TrayIcon is only fired if the "OK" button on the non-modal dialog is pressed, and not if the window is closed using the window close button.

Multiple calls to TrayIcon.displayMessage() will dismiss prior messages and leave only the last message. If the application is not in the foreground when TrayIcon.displayMessage() is called, the application bounces its icon in the Dock. Message windows are badged with the application's icon to identify the which application triggered the notification.

noah provided this sample:

java.awt.SystemTray.getSystemTray().add(new java.awt.TrayIcon(java.awt.Toolkit.getDefaultToolkit().getImage("foo.png")));

Note that you'll probably want to attach a menu to that icon before adding it to the tray, though.

SWT

According to documentation, SWT 3.3 and newer supports TrayItem icons on OSX.

Icons placed on the system tray will now appear when running on OS X in the status bar. SWT TrayItem

This snippet shows how to create a menu and icon and put them in the Tray.

R. Bemrose
This works for me on Snow Leopard: java.awt.SystemTray.getSystemTray().add(new java.awt.TrayIcon(java.awt.Toolkit.getDefaultToolkit().getImage("foo.png")));
noah
@noah: Whoops, yeah, I should have put in an example.
R. Bemrose
Thanks, I'll try it lter at home. I can't see any big difference with my code, maybe it's only a silly thing. I can't use SWT, but I'll try it likewise. Thanks.
Sinuhe