Java TrayIcon displayMessage 'may trigger ActionEvent' when does it/doesn't it?

1.3k views Asked by At

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);
            }
        });
    }
}
1

There are 1 answers

3
MadProgrammer On

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