Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 704
sinuhepop Avatar asked Dec 28 '09 18:12

sinuhepop


People also ask

How do I get to system tray on Mac?

The system tray is available in both desktop and mobile operating systems such as Windows, Linux, Mac OS, Android and iOS. The location of the tray depends on the operating system; it is at the bottom right corner in most Windows versions and is at the top right corner on Linux, Mac OS and Android.

What is the system tray called on a Mac?

The menu bar runs along the top of the screen on your Mac.

How do I remove system tray icons Mac?

Hold down the Command key, click and drag the icon you want to hide to this other section. Release the icon and it will be hidden, You're left with a little arrow on the Menu bar next to all the icons you've left visible.


2 Answers

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.

like image 153
Powerlord Avatar answered Oct 31 '22 11:10

Powerlord


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.

like image 40
Mike Avatar answered Oct 31 '22 10:10

Mike