7. Keep the application in the tray - fakeivchenko/lwjwae GitHub Wiki
A tray icon keeps the application running after its window is hidden. A global shortcut works while another application has the keyboard. In this step, closing the window hides it, and the tray menu or a shortcut brings it back.
-
Save a 32 by 32 pixel PNG image as
src/main/resources/app/tray.png. -
In
Main.java, add.closeAction(CloseAction.HIDE)to the builder inmain, before.build():.decorated(false) .closeAction(CloseAction.HIDE) .resource("app/index.html")
-
At the end of
setUp, add the tray icon and the shortcut:Application application = window.application(); application.tray( "app/tray.png", new TrayMenuItem("Show notes", window::focus), new TrayMenuItem("Quit", application::quit)); application.globalShortcut("CmdOrCtrl+Alt+N", window::focus);
-
Add the imports:
import dev.ivchenko.lwjwae.CloseAction; import dev.ivchenko.lwjwae.tray.TrayMenuItem;
-
Run the application and close the window. The application keeps running in the tray. Click Show notes in the tray menu, or press Ctrl+Alt+N (Command+Option+N on macOS), to bring the window back. Click Quit to end the application.
CmdOrCtrl stands for Command on macOS and Ctrl on other platforms. On
Wayland, the desktop asks the user to confirm a global shortcut the first time. If another
application holds the shortcut, globalShortcut throws ShortcutUnavailableException.
Previous: Export notes through a native dialog · Next: Show notifications