Bill Nye Bill Nye - 4 years ago 831
Java Question

How to make a Windows Notification in java

In Windows 10 there is a notification thing that opens in the bottom right if the screen and i find them quite useful. I was just wondering if there is any way to create them in java? If there is an external library could you link me to it? This is what they look like

enter image description here

Answer Source

I can successfully produce this result using this very simple sample code:

screenshot

import java.awt.*;
import java.awt.TrayIcon.MessageType;
import javax.swing.ImageIcon;

public class TrayIconDemo {

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

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

        //If the icon is a file
        Image image = Toolkit.getToolkit().createImage("icon.png");
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png"));
        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        //Let the system resizes 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);
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download