Java – Blank Icon in Windows 10 notifications

My java application displays its icons on the system tray using code that looks more or less like:

Toolkit mainToolkit = Toolkit.getDefaultToolkit();
SystemTray mainTray = SystemTray.getSystemTray();
Image trayIconImage = mainToolkit.createImage(getClass().getResource(resourcePath));
TrayIcon mainTrayIcon = new TrayIcon(trayIconImage);
mainTray.add(mainTrayIcon);

Sometimes I change this icon:

Image newImage = mainToolkit.createImage(getClass().getResource(otherPath));
mainTrayIcon.setImage(newImage);

My application sometimes needs to display some notifications (using balloon messages from tray icons):

mainTrayIcon.displayMessage(someCaption,msg,TrayIcon.MessageType.NONE);

All this code is actually simplified in some way, but it has a good grasp of this function

So everything is fine on Windows 7 But it turns out that it is displayed differently on Windows 10 On the notification, an icon is displayed on the left It is usually the current tray icon for my application, but sometimes it is just blank:

The red circle above (on the notification) is sometimes a blank icon, not the icon of my application (in the small red circle on the system tray) I don't know why it happened As far as I know, this only happens when the tray icon and notification message of the application change before the first notification (its icon is always displayed correctly) If a notification is displayed, the fade out is manually turned off / then the tray icon and notification of the application change, and the next notification (using the new message just set) will display the icon of the application correctly

Solution

Just encountered this problem and found the correct solution:

mainTrayIcon. Setimageautosize (true);

This is how to send notifications on Windows:

public static void sendNotification(String title,String subtitle,String pathToIcon) {
    SystemTray mainTray = SystemTray.getSystemTray();
    Image trayIconImage = Toolkit.getDefaultToolkit().getImage(pathToIcon);
    TrayIcon mainTrayIcon = new TrayIcon(trayIconImage);
    mainTrayIcon.setImageAutoSize(true);
    try {
        mainTray.add(mainTrayIcon);
        mainTrayIcon.displayMessage(title,subtitle,TrayIcon.MessageType.NONE);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Call sendnotification ("title", "subtitle", "icons / icon-128. PNG"); program

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>