System Tray (Menu Extras) icon in Mac Os using Java
Asked Answered
P

2

15

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.

Pancreas answered 28/12, 2009 at 18:36 Comment(2)
I have seen this work with Java on OS X 10.5 within the last year. Show your failing code please.Tore
Sadly, I have no access to my code now, but I can't paste it later if it's necessary. Is very similar (or equal) to this example: java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/… The question is SystemTray.isSupported() returns true, no exception is thrown, but there is no icon :(Pancreas
R
18

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.

Rizzo answered 28/12, 2009 at 20:6 Comment(4)
This works for me on Snow Leopard: java.awt.SystemTray.getSystemTray().add(new java.awt.TrayIcon(java.awt.Toolkit.getDefaultToolkit().getImage("foo.png")));Panarabism
@noah: Whoops, yeah, I should have put in an example.Rizzo
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.Pancreas
The snippet is now located in Eclipse's git repository.Domela
F
1

I ported a Windows application to my Mac with little difficulty. One thing I noticed is that the icons are in full, living color (not following the Mac convention). I'll need to add a little OS-specific code to convert myself. But this is a big step up from the DLL dependent Desktop integration version from earlier iterations of Java.

Fetial answered 29/3, 2011 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.