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:
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: