8

I'm trying to emulate Objective C's NSStatusItem functionality in Java. That is, I'm trying to write a Java app that will sit in the Mac's menubar, like this. Here's a link to Apple's documentation on StatusBar.

Any way to do this in Java?

user73171
  • 95
  • 1
  • 5

2 Answers2

10

Use java.awt.SystemTray. I've confirmed it works on OS X Lion. According to this question, it's also possible to do with SWT.

Example:

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

public class MenuBarIconTest {
    public static void main(String[] args) throws MalformedURLException {
        TrayIcon trayIcon = null;
         if (SystemTray.isSupported()) {
             // get the SystemTray instance
             SystemTray tray = SystemTray.getSystemTray();
             // load an image
             Image image = Toolkit.getDefaultToolkit().getImage(new URL("http://cdn1.iconfinder.com/data/icons/Hypic_Icon_Pack_by_shlyapnikova/16/forum_16.png"));
             // create a action listener to listen for default action executed on the tray icon
             ActionListener listener = new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     System.out.println("action");
                     // execute default action of the application
                     // ...
                 }
             };
             // create a popup menu
             PopupMenu popup = new PopupMenu();
             // create menu item for the default action
             MenuItem defaultItem = new MenuItem("Do the action");
             defaultItem.addActionListener(listener);
             popup.add(defaultItem);
             /// ... add other items
             // construct a TrayIcon
             trayIcon = new TrayIcon(image, "Tray Demo", popup);
             // set the TrayIcon properties
             trayIcon.addActionListener(listener);
             // ...
             // add the tray image
             try {
                 tray.add(trayIcon);
             } catch (AWTException e) {
                 System.err.println(e);
             }
             // ...
         } else {
             // disable tray option in your application or
             // perform other actions
             //...
         }
         // ...
         // some time later
         // the application state has changed - update the image
         if (trayIcon != null) {
             //trayIcon.setImage(updatedImage);
         }

    }
}
Community
  • 1
  • 1
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
  • @TrevorDixon cool solution, but is it possible to make something advanced like this one https://stackoverflow.com/questions/57030625/java-macos-system-tray-with-buttons-layouts-frames-etc ? – Tymur Berezhnoi Aug 14 '19 at 07:58
0

There is a NICE library for doing this ... best one I've seen that integrates the SWIFT ability for menubar access with JavaFX ... since FX has no native way to work out of the menubar.

Here is the link if you're interested.

Michael Sims
  • 2,360
  • 1
  • 16
  • 29