Continuing my previous post about setting system-tray icons, I will now show how to set informational popup messages next to these icons.
Asynchronous informational messages can be presented next to the sys-tray icon, in a fashion similar to what we came to expect from modern programs. This could be used to indicate some unexpected event that was detected, or the end of a complex calculation phase. The message title, text and severity icon are all customizable.
Unfortunately, the Java method used to display messages, java.awt.TrayIcon.displayMessage(), expects an object of type java.awt.TrayIcon.MessageType, which is an enumeration within the TrayIcon class. However, Matlab’s dot-notation does not recognize what should have been the following correct notation, so we need to resort to Java reflection:
>> trayIcon.displayMessage('title','info msg',TrayIcon.MessageType.INFO); ??? No appropriate method or public field MessageType for class java.awt.TrayIcon >> trayIconClasses = trayIcon.getClass.getClasses; >> trayIconClasses(1) ans = class java.awt.TrayIcon$MessageType <= hurray!!! >> MessageTypes = trayIconClasses(1).getEnumConstants MessageTypes = java.awt.TrayIcon$MessageType: [java.awt.TrayIcon$MessageType] <= 1: ERROR [java.awt.TrayIcon$MessageType] <= 2: WARNING [java.awt.TrayIcon$MessageType] <= 3: INFO [java.awt.TrayIcon$MessageType] <= 4: NONE >> trayIcon.displayMessage('title','info msg',MessageTypes(3));
and another example, now with a WARNING icon:
If the title string is left empty, then neither title nor the severity icon will be displayed. The message can still be manually dismissed by clicking within its boundaries:
Informational popup messages are automatically aligned and positioned by the system. Messages are automatically dismissed by the system after some time, if not dismissed by the user first. The exact time is determined by system and user activity and other such external factors. Informational messages replace one another, if the previous message has still not been cleared by the user.
I have created a utility function called SYSTRAY, which is a convenience function that facilitates the setup and update of system tray icons and messages. SYSTRAY (with source code) can be downloaded from the File Exchange.
I would be happy to hear if and how you’re using the new system-tray functionality in your application – let me know below.