52

In Windows 10 there is a notification that opens in the bottom right of the screen and I find them quite useful.

Is there is any way to create Windows notifications in Java? This is what they look like:

sample of windows notification desired

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Bill Nye
  • 831
  • 1
  • 10
  • 15

3 Answers3

105

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

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);
    }
}
SleightX
  • 43
  • 1
  • 9
randers
  • 5,031
  • 5
  • 37
  • 64
  • 1
    One question how would i set the exclamation mark to it's own texture, i have seen other notifications with custom png files as there icons. – Bill Nye Dec 28 '15 at 10:07
  • It does not look like that's possible - that seems to be a feature not available in AWT, it's possibly only available in the native windows gui toolkits. – randers Dec 28 '15 at 10:09
  • 9
    Currently the notification is removed from the action center after it expires. Is there a way to keep it there? – Blaine Jan 28 '17 at 03:27
  • Super answer. Is there a way to stop the system tray icon from preventing JVM from exiting? – Johannes Brodwall Sep 11 '20 at 14:12
  • 3
    @JohannesBrodwall if you remove the `trayIcon` from the `tray` (`tray.remove(trayIcon)`) the JVM will exit. Keep in mind that if you remove it while the notification is still open, the notification will *magically* disappear. – MrPowerGamerBR Dec 14 '20 at 19:32
5

This can be achieved with the SystemTray and TrayIcon classes. Also, if this is a new API for you, you might want to check the dedicated tutorial "How to Use the System Tray".

Lolo
  • 4,277
  • 2
  • 25
  • 24
1

you can use Notifications with javafx easily with Notification class java.lang.Object org.controlsfx.control.Notifications

public static showNotification(String title,String text){
Notifications notificationTest=Notifications.create();
notificationTest.position(Pos.BASELINE_RIGHT);
notificationTest.title(title);
notificationTest.text(text);
notificationTest.show();//for error noti notificationTest.showError();

}

https://www.javadoc.io/static/org.controlsfx/controlsfx/8.40.16/org/controlsfx/control/Notifications.html
belmel ahmed
  • 176
  • 1
  • 5