Как сделать Windows Notification в Java

В Windows 10 есть уведомление, которое открывается в правом нижнем углу экрана, и я считаю их весьма полезными.

Есть ли способ создания уведомлений Windows на Java? Вот как они выглядят:

sample of windows notification desired

Ответы

Ответ 1

Я могу успешно получить этот результат, используя этот очень простой пример кода:

screenshot

import java.awt.*;
import java.awt.TrayIcon.MessageType;

public class TrayIconDemo {

    public static void main(String[] args) throws AWTException {
        if (SystemTray.isSupported()) {
            TrayIconDemo td = new TrayIconDemo();
            td.displayTray();
        } else {
            System.err.println("System tray not supported!");
        }
    }

    public void displayTray() throws AWTException {
        //Obtain only one instance of the SystemTray object
        SystemTray tray = SystemTray.getSystemTray();

        //If the icon is a file
        Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png"));

        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        //Let the system resize the image if needed
        trayIcon.setImageAutoSize(true);
        //Set tooltip text for the tray icon
        trayIcon.setToolTip("System tray icon demo");
        tray.add(trayIcon);

        trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
    }
}

Ответ 3

Правильный ответ работает для меня в jdk 1.8 с помощью Toolkit.getDefaultToolkit() вместо Toolkit.getToolkit()