Question

So what I want is when my program runs (It's a system tray) one of those small notification problems show up at the bottom right of the screen. I tried.

trayIcon = new TrayIcon(image, "Title", popup);
trayIcon.setImageAutoSize(true);
trayIcon.displayMessage("Title", "MESSAGE HERE", TrayIcon.MessageType.ERROR) //THIS IS THE LINE THAT SHOULD SHOW THE MESSAGE

Where should it be for it to run when the program runs and is that the correct method with the correct parameters?

Était-ce utile?

La solution

Have you had a read on How to Use the System Tray?

However

Check this minimal example:

After tray.add(trayIcon); than show your message.

import java.awt.AWTException;
import java.awt.Graphics2D;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.SwingUtilities;

public class Test {

    public Test() throws Exception {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new Test();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    private void initComponents() throws Exception {
        createAndShowTray();
    }

    private void createAndShowTray() throws Exception {
        //Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }

        //retieve icon form url and scale it to 32 x 32
        final TrayIcon trayIcon = new TrayIcon(resizeImage(ImageIO.read(
                new URL("http://www.optical-illusions.in/illusions/blue_rotation_optical_illusion.jpg")), BufferedImage.TYPE_INT_ARGB, 32, 32));

        //get the system tray
        final SystemTray tray = SystemTray.getSystemTray();

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

        trayIcon.displayMessage("Title", "MESSAGE HERE", TrayIcon.MessageType.ERROR); //THIS IS THE LINE THAT SHOULD SHOW THE MESSAGE

    }

    private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {
        BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
        g.dispose();

        return resizedImage;
    }
}

Autres conseils

Go to the little action center and click "manage notificaitons" then scroll down in the settings until you get to the

enter image description here

It didn't work when I had it in a loop so I just tried putting an additional statement outside the loop and then it worked. Yay!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top