Creating a menu bar icon without having a visible running application associated with it

173 Views Asked by At

I want to create a menu bar icon that is not associated with a running application. Just like dropbox's menu bar icon doesn't appear as an open application, but only on the menu bar.

What I got so far is the code for adding the menu bar icon:

package tray;

import java.awt.AWTException;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.io.IOException;

import javax.imageio.ImageIO;

import logging.Logger;

public class TrayObject {
    private TrayIcon trayIcon;

    public TrayObject() {
        if(SystemTray.isSupported()) {
            try {
                trayIcon = new TrayIcon(
                            ImageIO.read(getClass().getResource("/Add.png")), 
                            "CodeLibrary");
                SystemTray.getSystemTray().add(trayIcon);
            } catch (IOException e) {
                Logger.log(this, "IOException");
            } catch (AWTException e) {
                Logger.log(this, "AWTException");
            }   
        }
    }

    public static void main(String[] args) {
        TrayObject to = new TrayObject();
    }
}

When I run the application the menu bar icon is showing as it should.

Image of my system tray

However, the problem is that I don't want the application 'TrayObject' to have a menu bar nor be visible when tabbing among applications. I want the menu bar icon to be there without being associated with the running application 'TrayObject'. Is it possible to do this in Java, and if so, how?

1

There are 1 best solutions below

0
Ian Roberts On

If you wrap the application up as a .app bundle using appbundler then you can add the LSUIElement property in Info.plist to suppress the dock icon and menu bar.

<key>LSUIElement</key>
<string>1</string>

(see this apple.stackexchange question for an example)