0

I'm writing an application that utilizes Java's TrayIcon class, but I can't seem to get it to display messages on Windows XP/Vista. Is this a known issue or is there something that I'm missing? (Messages appear as they should on Windows 7)

Code:

public class SysTray {
    public static void main(String[] args) throws Exception {
        TrayIcon icon = new TrayIcon(getImage());

        icon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Good");
            }
        });
        SystemTray.getSystemTray().add(icon);

        icon.displayMessage("Attention", "Please click here", 
            TrayIcon.MessageType.WARNING);
    }

    private static Image getImage() throws HeadlessException {
        Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
        Image img = new BufferedImage(defaultIcon.getIconWidth(), 
            defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

        return img;
    }
}

Edit: computers were both using the latest version of Java

Edit II: Results of tests on various computers:

  • Windows XP, J7_25 (1.7.0_25-b16): Works
  • Windows 7, J7_25 (1.7.0_25-b17): Works
  • Windows XP, J7_25 (1.7.0_25-b17): Does not work
Timr
  • 1,012
  • 1
  • 14
  • 29
  • no there isn't any issue with displayMessage() on WinXP/7/8 (never touched Vista) on > JDK_6.18 – mKorbel Jul 21 '13 at 21:15
  • @mKorbel That's not (shouldn't be) an issue here - both computers are on a version > 6.18 – Timr Jul 21 '13 at 21:27
  • sorry your comment isn't more clear than your question, btw just my curiosity, nothing else (otherwise I'd be ask for an [SSCCE](http://sscce.org/), short, runnable, compilable and caused a.m. lack on WinXP) StringConstants are from C++/.Net invoked from JNI/A or is some hand_Made – mKorbel Jul 21 '13 at 21:35
  • @mKorbel `StringConstants` is just a hand made class with.. well. Strings that are constant – Timr Jul 21 '13 at 22:44
  • Show the simplest possible runnable code which _should_ do what you expect it to. – Thorbjørn Ravn Andersen Jul 21 '13 at 22:45

1 Answers1

1

see whats happened (Java6_022 and Java7_21, tested on both JDKs)

WinXP - -> enter image description here

Win8 - -> enter image description here

from code

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.metal.MetalIconFactory;

public class SysTray {

    public SysTray() {
        TrayIcon icon = new TrayIcon(getImage());
        icon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Good");
            }
        });
        try {
            SystemTray.getSystemTray().add(icon);
        } catch (AWTException ex) {
        }
        icon.displayMessage("Attn","Click here",TrayIcon.MessageType.WARNING);
    }

    private Image getImage() throws HeadlessException {
        Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
        Image img = new BufferedImage(defaultIcon.getIconWidth(),
                defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        defaultIcon.paintIcon(new JPanel(), img.getGraphics(), 0, 0);
        return img;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                SysTray sysTray = new SysTray();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • sorry I haven't - `Windows XP, J7_25 (1.7.0_25-b17): Does not work`, [then search and to report](http://search.oracle.com/search/search?search_p_main_operator=all&start=1&group=bugs.sun.com&q=trayicon) – mKorbel Jul 23 '13 at 20:26
  • I instaled latest 1.7.0_25-b17 win32b, no issue, compiled in JDK1.6_022 and JDK1.7._011 – mKorbel Jul 24 '13 at 06:23