Mac status bar but not on Dock

I am trying to make a Java application on a Mac that places an icon in the status bar, but I donโ€™t need the jar icon on the Mac dock (the one that has a cup of coffee on paper). So I tried to use the System.setProperty technique (java.awt.headless, true), but then I canโ€™t put anything in the SystemTray menu because I get a HeadlessException. If someone knows a way around this, help will be appreciated.

+4
source share
2 answers

it's easy ... if you know how :)

first wrap your jar file in the mac app pack

then go to the contents of your generated package and open the info.plist file. just add the LSUIElement property and set it to 1. this will remove the application from the dock at startup. also see apple docs here: http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html


for completeness: there is another way to do this, but it is much more painful. there is a cocoa command that allows you to dynamically display / hide the dock icon: SetSystemUIMode (https://developer.apple.com/library/mac/#documentation/Carbon/reference/Dock_Manager/Reference/reference.html) you can either try call this command with rococoa, or write your own jni lib. alternatively, I would have an xcode project that would do something very similar to the menu bar in my github account: https://github.com/kritzikratzi/jAppleMenuBar/ you will need to change some parameters in the src / native / file jAppleMenuBar.m.

+4
source

This avoids anything in the dock:

System.setProperty("apple.awt.UIElement", "true"); 

And that adds a 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."); } 
0
source

Source: https://habr.com/ru/post/1413281/


All Articles