- Undocumented Matlab - https://undocumentedmatlab.com -

Setting system tray popup messages

Posted By Yair Altman On March 31, 2009 | 21 Comments

Continuing my previous post about setting system-tray icons [1], 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));

systray INFO message

and another example, now with a WARNING icon:
systray WARNING message

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:

systray messages without a title (hence also without a severity icon)
systray messages without a title (hence also without a severity icon)

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 [2].
I would be happy to hear if and how you’re using the new system-tray functionality in your application – let me know below.

Categories: Desktop, GUI, Icons, Java, Low risk of breaking in future versions


21 Comments (Open | Close)

21 Comments To "Setting system tray popup messages"

#1 Comment By Jason McMains On July 10, 2009 @ 08:06

Yair,
Thanks again for this utility, just thought I would let you know that I’m using this to replace any helpdlg boxes that I previously used to give process direction during a program, like click here, or define this range. Using this, a user can breeze through a program if they know what to do, or go slowly and read the pop-ups.

#2 Pingback By Updating speaker sound volume | Undocumented Matlab On October 15, 2009 @ 09:14

[…] Unfortunately, Matlab prevents using Java classnames containing a period (e.g., Port.Info) or static Interface values, so we cannot directly access Port.Info.SPEAKER or FloatControl.Type.VOLUME. I’ve reported this bug to Mathworks earlier this year, and I do not know in which Matlab release (if at all) they intend to fix it. Until then we need to use workarounds, as in the code above. I posted a similar issue (and its workaround) earlier in this blog, when setting system-tray popup message. […]

#3 Comment By Albert On December 17, 2009 @ 05:11

On the version (R2007b) of Matlab I use this statement does not work:
trayIconClasses = trayIcon.getClass.getClasses;
??? No method ‘getClass’ with matching signature found for class ‘java.awt.TrayIcon’.

Is it supprted on later versions?

#4 Comment By Yair Altman On December 27, 2009 @ 15:53

Albert – it should work on Matlab installations that run JVM 1.6+. To determine your JVM version, type the following in your Command Window:

version -java

If this is not helpful, try to inspect the trayIcon object using the methodsview function, to see why the getClass method fails:

methodsview(trayIcon);

#5 Comment By andandgui On June 4, 2010 @ 12:29

i couldn’t run SoundVolume function on Matlab.. When i write SoundVolume=10; then nothing happens .. How can i run the SoundVolume function which is written at

[9]

#6 Comment By Yair Altman On June 5, 2010 @ 11:19

@andandgui – did you even read the SoundVolume help?

SoundVolume is not a variable that you can assign like SoundVolume=10, but a function that accepts input parameters like SoundVolume (1.0) and it only accepts values between 0.0-1.0 so 10 is not a valid value.

Also, next time, please post the question in the relevant page ( [9]) – your question has nothing to do with this page.

#7 Comment By Peter On June 16, 2010 @ 02:54

Dear Yair

This looks like a great utility. Unfortunately it does not work properly on my system (Matlab 2010a, Windows XP, Java 1.6.0_12-b04). To start with I call

hIcon = systray('C:\Program Files\MATLAB\R2010a\toolbox\matlab\icons\help_ex.png');

Afterwards, the only command that works is the set command for deletion. Setting message pop-ups and the like does not work (does not yield any error messages, but does not affect the icon).

Furthermore, if I click the icon in the system tray I get the exception pasted below.

Have you any suggestions on how to get around this. I have tried the exception fix you link to on the uisplitpane post, but that does not help either. Thanks for a great site and lots of cool applications

Peter

Exception in thread “AWT-EventQueue-0″ java.lang.ClassCastException: 
java.awt.TrayIcon cannot be cast to java.awt.Component
at com.mathworks.widgets.tooltip.ToolTipAndComponentAWTListener.mouseInTipComponent(ToolTipAndComponentAWTListener.java:175)
at com.mathworks.widgets.tooltip.ToolTipAndComponentAWTListener.eventDispatched(ToolTipAndComponentAWTListener.java:125)
at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source)
at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
...
at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
at java.awt.Toolkit.notifyAWTEventListeners(Unknown Source)
at java.awt.TrayIcon.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

#8 Comment By Mike On September 10, 2011 @ 05:31

That looks like a “bug.” I’ll ask them to fix it, but the earliest it would be done would be 12b, if at all…

#9 Comment By Yair Altman On September 10, 2011 @ 09:44

@Mike – thanks – couldn’t ask for anything more

#10 Pingback By Setting status-bar text | Undocumented Matlab On June 23, 2010 @ 03:09

[…] Note: Users interested in non-intrusive status-bar messages, may also be interested in another non-intrusive messaging mechanism which I have described some months ago: Setting system tray popup messages. […]

#11 Comment By Alexander On September 9, 2011 @ 05:58

Dear Yair,

as all the others I’d like to thank you for the wonderful systray tool. It’s really great.

While I implemented it these days in an old 2007b installation, it worked without any problem. (At least after having included the solution for the messages as pointed out in your uisplitpane entry).

Now I’m trying to migrate to 2010bSP1 and stumbled over two problems:

a) Java complained about setting the popup entry enabled using your line

set(menuItem,'Enabled',get(thisItem,'Enable'));

As I’m not familiar with java: Is the workaround

menuItem.setEnabled(strcmp(get(thisItem,'Enable'),'on'));

valid?

b) I get the same error message as Peter mentioned above when trying to access the popup menu:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.TrayIcon cannot be cast to java.awt.Component
	at com.mathworks.widgets.tooltip.ToolTipAndComponentAWTListener.mouseInTipComponent(ToolTipAndComponentAWTListener.java:177)
	at com.mathworks.widgets.tooltip.ToolTipAndComponentAWTListener.eventDispatched(ToolTipAndComponentAWTListener.java:128)
	at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit.notifyAWTEventListeners(Unknown Source)
	at java.awt.TrayIcon.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

As I learned, this is an Java 1.6 issue. Is there a workaround on the Matlab-level for systray available? Or what else could be done?

Best wishes,

Alexander

#12 Comment By Alexander On September 9, 2011 @ 07:16

Update:

Going through the implementation in 2010b I found a second problematic point. In line 232, the “ActionPerformedCallback” is set. This also drops an warning. Now I found on of your infos on using set/get with core java object in the entry on Uicontrol callbacks. Is it thus reasonable to change

set(menuItem,'ActionPerformedCallback',get(thisItem,'Callback'));

to

set(handle(menuItem,'CallbackProperties'),'ActionPerformedCallback',get(thisItem,'Callback'));

If so, I’d suggest to make an update of the FileExchange version (also including the comments made in the uisplitpanel entry). If not, I’m really interested in a workaround.

Alexander

#13 Comment By Yair Altman On September 9, 2011 @ 07:42

@Alexander – using menuItem.setEnabled() is indeed safe and valid, and in fact better than using set(…). Your fix for using CallbackProperties is also correct – this is described in [10].

Unfortunately, I do not have a solution for the popup message errors at this time. If I do, I will post it on this page.

#14 Comment By fahad On December 8, 2011 @ 02:01

thanks for the code and the explanation with picures

#15 Comment By Thierry Dalon On January 10, 2013 @ 06:42

Hi Yair
nice utility!
In the post you don’t provide the code for the multi-line example.
Do you use char(10) for breaking lines? Example:

systray(hIconTray,'Message',{'title',['informational message',char(10),'Second line'],'info'})

It may be nice if the systray function would accept as message as cellstr and breaking lines automatically like usual in Matlab msgbox.
Example:

systray(hIconTray,'Message',{'title',{'informational message','Second line'},'info'})

#16 Comment By Yair Altman On January 10, 2013 @ 10:22

@Thierry – indeed, char(10) can be used to break lines in the systray message.

You might also try to use [11] with a <br/> tag (untested, but I think it should work).

Cell arrays are currently not supported unfortunately.

#17 Comment By Thierry Dalon On January 11, 2013 @ 05:41

One could convert cellstr in multi-lines char with one line:
msg={‘Another’,’multi-line’,’Example’}
sprintf(‘%s\n’,msg{:})
so one can then pass it to systray.

#18 Comment By Thierry Dalon On January 16, 2013 @ 03:13

For handling recurring warnings because of java exceptions I’ve come to the solution to clear the java events on mouse click. I had also to update the list of exceptions. See extracted code below:

set(trayIcon,'MousePressedCallback',@clearexc)

function clearexc()
   t = java.awt.Toolkit.getDefaultToolkit();
   ev = t.getAWTEventListeners();
   for i = 1 : length(ev)
      str = ev(i).getListener().getClass.toString;  
      if str.indexOf('com.mathworks.mwswing.desk.DTSelectionManager') >= 0 || ... % see https://undocumentedmatlab.com/blog/uisplitpane/#comment-658
         str.indexOf('javax.swing.plaf.basic.BasicLookAndFeel') >= 0 || ...
         str.indexOf('com.mathworks.widgets.text.mcode.MLintDecorator') >= 0 || ...
         str.indexOf('com.mathworks.widgets.desk.DTSelectionManager') >=0 || ... % TD
         str.indexOf('com.mathworks.widgets.grouptable.ToolTipSupport') >=0 || ... % TD
         str.indexOf('com.mathworks.widgets.text.STPMessagePanel') >=0 || ... % TD
         str.indexOf('com.mathworks.widgets.text.MWToolTipSupport') >=0 || ... % TD->crashes on double-click
         str.indexOf('com.mathworks.widgets.tooltip') >=0, % TD
            t.removeAWTEventListener(ev(i));
            %disp(str)
      end
   end  % for ev
end  % function clearexc

#19 Comment By Reymon On July 24, 2014 @ 03:33

I used your code but I have eccepetion with this too.

When I use this code this code any exception. Is this true?

set(tray,'MousePressedCallback',@clearexc)
t = java.awt.Toolkit.getDefaultToolkit();
ev = t.getAWTEventListeners();
for i = 1 : length(ev)
    str = ev(i).getListener().getClass.toString;
    t.removeAWTEventListener(ev(i));
end

Is any problem with this code?

Thanks

#20 Comment By Thierry Dalon On January 16, 2013 @ 03:14

I could not find a way to delete/clear the current popup message…

#21 Comment By Yair Altman On January 23, 2013 @ 04:30

@Thierry – the popup is in general automatically removed by the operating system after several seconds. You can always set the popup message to empty (see the code in systray utility (setIconMessage() sub-function).

You can also delete and recreate the systray to get the same effect as workaround. Read the code in the systray utility to see how (basically, java.awt.SystemTray.getSystemTray.remove(jcomp)).


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages

URLs in this post:

[1] setting system-tray icons: http://undocumentedmatlab.com/blog/setting-system-tray-icons/

[2] downloaded from the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/23299

[3] Setting system tray icons : https://undocumentedmatlab.com/articles/setting-system-tray-icons

[4] Customizing help popup contents : https://undocumentedmatlab.com/articles/customizing-help-popup-contents

[5] Matlab toolstrip – part 9 (popup figures) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures

[6] Sending email/text messages from Matlab : https://undocumentedmatlab.com/articles/sending-email-text-messages-from-matlab

[7] Setting status-bar text : https://undocumentedmatlab.com/articles/setting-status-bar-text

[8] Better MEX error messages : https://undocumentedmatlab.com/articles/better-mex-error-messages

[9] : https://undocumentedmatlab.com/blog/updating-speaker-sound-volume/

[10] : https://undocumentedmatlab.com/blog/uicontrol-callbacks/#memory_leak

[11] : https://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.