Why does Java's AWT's tray icon swallow events when displaying a message?
Asked Answered
I

0

0

Using Java 8 and AWT I'm displaying a tray icon on Windows 10. I also use the tray icon to display notifications. While displaying this notification, the mouse listener in the icon won't issue the first pressed and clicked event.

Here's the minimum example:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TrayIconDemo {
    public static void main(String[] args) {
        createTrayIcon();
    }

    private static void createTrayIcon() {
        Image image = Toolkit.getDefaultToolkit().getImage(TrayIconDemo.class.getResource("/foo.png"));
        final TrayIcon trayIcon = new TrayIcon(image);

        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                System.out.println("Mouse Clicked");
            }

            public void mousePressed(MouseEvent e) {
                System.out.println("Mouse Pressed");
            }

            public void mouseReleased(MouseEvent e) {
                System.out.println("Mouse Released");
            }
        });

        try {
            SystemTray.getSystemTray().add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
            return;
        }

//        trayIcon.displayMessage("title", "message", TrayIcon.MessageType.NONE);
    }
}

If you run the example as is and click the icon twice, you get the expected six events (three for each click):

Mouse Pressed
Mouse Released
Mouse Clicked
Mouse Pressed
Mouse Released
Mouse Clicked

Now, if you uncomment the last line, the one that calls displayMessage, then you get the little message:

enter image description here

but if you click the icon twice, instead you get:

Mouse Released
Mouse Pressed
Mouse Released
Mouse Clicked

The initial pressed and clicked events are swallowed somewhere. Why is that?

When you click the tray icon, the message doesn't go away. If you dismiss the message, then the tray icon works as expected with all 6 events.

Here's a video showing the issue:

https://www.youtube.com/watch?v=eV2Lwim2EcY

Iphigenia answered 10/5, 2018 at 17:28 Comment(3)
For me still prints all 6 lines, even after uncommenting the last line, can you elaborate the procedure more ?Suggestion
@aKilleR: what version of Windows are you using? I'm using Windows 10 Pro, in case that's relevant.Iphigenia
@aKilleR: I'm not sure what else I could have added, so, I recorded a video of my issue.Iphigenia

© 2022 - 2024 — McMap. All rights reserved.