How to create and display a Windows 10 Notification with Java AWT

How to create and display a Windows 10 Notification with Java AWT

In Java it's pretty confusing how to generate different types of notifications or alert. Some developers prefer to use the JOptionPane, however when you work on a fixed environment, for example in Windows 10, it's pretty nice to use the default notification styles of Windows, so that's why we'll show you a sweet and short snippet to display a Windows 10 notification easily with Java AWT.

The following code generates the desired notification in the system tray, so you can simply create a method for it, wrap it inside your code or just changing the text of the alerts and that's it:

import java.awt.*;
import java.awt.event.*;
import java.awt.TrayIcon.MessageType;
import java.net.MalformedURLException;

try{
    //Obtain only one instance of the SystemTray object
    SystemTray tray = SystemTray.getSystemTray();

    // If you want to create an icon in the system tray to preview
    Image image = Toolkit.getDefaultToolkit().createImage("some-icon.png");
    //Alternative (if the icon is on the classpath):
    //Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png"));

    TrayIcon trayIcon = new TrayIcon(image, "Java AWT 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);

    // Display info notification:
    trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.INFO);
    // Error:
    // trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.ERROR);
    // Warning:
    // trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.WARNING);
}catch(Exception ex){
    System.err.print(ex);
}

Note that the execution of the code needs to be done through a Try-Catch statement that either catchs the 2 exceptions thrown by the code (AWTException, MalformedURLException) or a general Exception (as shown above).

Structured example

The following example, shows a very simple application class that draws a simple button in a Frame. When the button is clicked, a tray notification will appear:

package sandbox;

import java.awt.*;
import java.awt.event.*;
import java.awt.TrayIcon.MessageType;
import java.net.MalformedURLException;

public class Sandbox {
    
    /**
     * Parsing a JSONObject string
     * 
     * @param args 
     */
    public static void main(String[] args) {
        Sandbox app = new Sandbox();
    }
    
    public Sandbox(){
        Frame f = new Frame("Button Example");  
        Button btn = new Button("Click Here");  
        btn.setBounds(50,100,80,30);  
        f.add(btn);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);
        
        Sandbox _this = this;
        
        btn.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (SystemTray.isSupported()) {
                    try{
                        _this.displayTray();
                    }catch(AWTException ex){
                        
                    }catch(MalformedURLException ex){
                    
                    }
                } else {
                    System.err.println("System tray not supported!");
                }
            }
        });
    }
    
    public void displayTray() throws AWTException, MalformedURLException {
        //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, "Java AWT 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", "Java Notification Demo", MessageType.INFO);
    }
}

The previous code will generate the following frame and display the notification:

Java Notification Example

Happy coding !

This could interest you

Become a more social person