简体   繁体   中英

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

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.

我的系统托盘的图像

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM