Mac status bar item but not on Dock
Question
I'm trying to make a java application on Mac that puts an icon in the status bar, but I don't want the jar icon on the Mac dock (the one that has the coffee cup on the paper). So I tried to use the System.setProperty(java.awt.headless, true) technique, but then I can't put anything in the SystemTray menubar because I get an HeadlessException. If anybody knows a way to get around this, help would be appreciated.
Solution
it's easy... if you know how :)
first wrap your jar file in a mac application bundle
- either use the "jar bundler" app that comes with the mac developer tools
- or use the -unrelated- jarbundler ant task http://jarbundler.sourceforge.net/
then go into the contents of your generated package and open the info.plist. there just add the LSUIElement property and set it to 1. this removes the application from the dock when started. also see the apple docs here: http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html
for completeness: there's also another way to do this, but it's much more painful. there is a cocoa command that allows you to show/hide the dock icon dynamically: SetSystemUIMode (https://developer.apple.com/library/mac/#documentation/Carbon/reference/Dock_Manager/Reference/reference.html) you could either try to call this command using rococoa or write your own jni lib. alternatively i would have an xcode project which does something very similar -hide the menu bar- in my github account: https://github.com/kritzikratzi/jAppleMenuBar/ you'd only have to change some parameters in the src/native/jAppleMenuBar.m file.
OTHER TIPS
This avoids anything in the dock:
System.setProperty("apple.awt.UIElement", "true");
And this adds the tray icon, as shown in https://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon =
new TrayIcon(createImage("images/bulb.gif", "tray icon"));
final SystemTray tray = SystemTray.getSystemTray();
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
Menu displayMenu = new Menu("Display");
MenuItem errorItem = new MenuItem("Error");
MenuItem warningItem = new MenuItem("Warning");
MenuItem infoItem = new MenuItem("Info");
MenuItem noneItem = new MenuItem("None");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(cb1);
popup.add(cb2);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(errorItem);
displayMenu.add(warningItem);
displayMenu.add(infoItem);
displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
}