javapopupmenusystem-traytraysystray

Why isn't the popup menu for my System Tray Icon responding to inputs?


I've been working on a background java program for a while and its almost ready to be released so I thought I should probably add a way to exit the program. I have this function:

private static void setupSysTray() {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
            return;
    }

    try {
        final PopupMenu popupMenu = new PopupMenu();
        final TrayIcon trayIcon = new TrayIcon(ImageIO.read(new File(workingDirectory +
 fileSeparator + "tray.png")), "Multi");
        final SystemTray tray = SystemTray.getSystemTray();

        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(e -> {
            System.out.println("Something happened!");
            System.exit(0);
        });
        popupMenu.add(exitItem);
        trayIcon.setPopupMenu(popupMenu);

        tray.add(trayIcon);
    } catch (IOException | AWTException e) {
        e.printStackTrace();
    }
}

I presumed this would handle it, I pieced it together from various stack overflow posts and the offical documentation. The result is that the tray icon appears, with the correct image and tooltip. When I right click it I see the menu item for "exit" show up. But that's where it breaks, the menu item doesn't have any hover coloring (leading me to believe input at all with it is broken) and clicking on the item turns up no results. Have I made some silly mistake like ordering the adding of items wrong? What's going on here?


Solution

  • As it turns out I was making use of a Global Mouse Hook in an old part of the code that I had all but forgotten about. The hook is from this repository and the fix was changing the hook's raw input setting from true to false.

    // True instead of false seems to block JavaFX events
    private static GlobalMouseHook mouseHook = new GlobalMouseHook(false);