3

When using TrayIcon.displayMessage to show a popup notification, the Java 6 documentation states that 'Clicking on the message may trigger an ActionEvent.'

http://docs.oracle.com/javase/6/docs/api/java/awt/TrayIcon.html#displayMessage%28java.lang.String,%20java.lang.String,%20java.awt.TrayIcon.MessageType%29

'May'? Thanks, documentation.

On my Windows 2000 test VM, clicking on the message does not appear to trigger an ActionEvent (unfortunately I don't own any newer Windows licenses to test), while the same code does trigger one in Ubuntu and OS X.

Note: Clicking on the icon itself does trigger an event on the mouse listener.

So anyway, my specific questions are:

  1. Am I correct that clicking the notification does not trigger an ActionEvent in Windows 2000, or is there something I'm doing wrong?

  2. Does it work to trigger an ActionEvent in Windows XP or Windows 7?

Minimal sample code is below. When I run this using java Test in Windows 2000, clicking on the notification does not generate any command-line output.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.SwingUtilities;

public class Test
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TrayIcon icon = new TrayIcon(
                    new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB));
                icon.addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent arg0)
                    {
                        System.err.println("ActionEvent: " + arg0);
                    }
                });
                try
                {
                    SystemTray.getSystemTray().add(icon);
                }
                catch(AWTException e)
                {
                    e.printStackTrace();
                }
                icon.displayMessage("New message", "Can you click on this?",
                    TrayIcon.MessageType.INFO);
            }
        });
    }
}
sam
  • 2,105
  • 2
  • 15
  • 18

1 Answers1

1

The problem you're dealing with is a cross platform implantation issue, that's why it 'may' trigger an event

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I understand that, but I think it would be useful to know (for me and others) which platforms it actually does trigger an event on. – sam Jul 27 '12 at 16:36
  • Clearly the notification does cause an event of some kind, because C applications respond when I click, but Java doesn't. I'm using windows 10 btw. – Matthew Oct 02 '17 at 21:04
  • @Matthew Because Java is has distilled the requirements down to a cross platform API, just because it works on Windows in C doesn't mean it will work the same way on other platforms - the Java API is making it's own choices over when an event should be triggered based on the context of the API itself - not the platform – MadProgrammer Oct 02 '17 at 21:12