how can one assign icons to a figures uimenu entries, the same way it
is done for the Matlab desktop ->Window->Tile etc. All the menu
entires have an empty space in front of them, so they are obviously
prepared to have icons there, but how?
Cheers
Dani
Is the empty space definitely for an icon, and not for showing the
associated accelerator key? I note that the last example shown in the
uimenu documentation is,
uimenu(f,'Label','Quit','Callback','exit',...
'Separator','on','Accelerator','Q');
which is suggestive of accelerators, not of icons.
the accelerator key is displayed behind the menu entry, not before.
There is an old newsgroup entry by Yair that states that icons can be
used through java, but it looks like Matlab does not give the
possibility to access this functionality.
Dani
This can't be done using plain supported Matlab but it can be done using undocumented Java functionality. Matlab menus (uimenu) are basically simple wrappers for the much more powerful and flexible Java Swing JMenu and JMenuItem on which they are based. Much important functionality that is available with Java menus is missing from the Matlab uimenus, including menu icons.
Let's add an icon to the Save menu item. We need a reference for the "Save" menu item. Note that unlike regular Java components, menu items are retrieved using the getMenuComponent() method, not getComponent():
% File main menu is the first main menu item => index=0
jFrame = get(handle(gcf),'JavaFrame');
jMenuBar = jFrame.fFigureClient.getMenuBar;
jFileMenu = jMenuBar.getComponent(0);
% Save menu item is the 5th menu item (separators included)
jSave = jFileMenu.getMenuComponent(4); %Java indexes start with 0!
inspect(jSave) => just to be sure: label='Save' => good!
Let us now set an icon for the Save menu item. Many of Matlab's icons reside in either the [matlabroot '/toolbox/matlab/icons/'] folder or the [matlabroot '/java/jar/mwt.jar'] file (a JAR file is simply a zip file that includes java classes and resources like icon images. Let us an create icon from the latter, to keep a consistent lookand-feel with the rest of Matlab (we could just as easily use our own external icon files):
% External icon file example
myIcon = 'C:\Yair\save.gif';
jSave.setIcon(javax.swing.ImageIcon(myIcon));
% JAR resource example
jarFile = fullfile(matlabroot,'/java/jar/mwt.jar');
iconsFolder = '/com/mathworks/mwt/resources/';
myIcon = ['jar:file:/' jarFile '!' iconsFolder 'save.gif'];
myIcon = java.net.URL(myIcon); % not necessary for external files
jSave.setIcon(javax.swing.ImageIcon(myIcon));
I plan to post a couple of articles on similar Matlab menu customizations on the Undocumented-Matlab blog in a few weeks, so stay tuned...
Yair Altman
http://UndocumentedMatlab.com
??? Java exception occurred:
java.lang.ArrayIndexOutOfBoundsException: No such child: 4
at java.awt.Container.getComponent(Unknown Source)
at javax.swing.JMenu.getMenuComponent(Unknown Source)
Is this due to my using 2007b? I tried looking at other (lower) indexes and the same error occurs. Thanks.
It only works after opening the File menu. This can be done interactively or programmatically:
jFileMenu.doClick; % open the File Menu
jFileMenu.doClick; % close the menu
Yair Altman
http://UndocumentedMatlab.com
first of all, thanks to Yair Altman and his great work finding all those undocumented matlab functions.
The workaround to set an uimenu icon works but unfortunatelly matlab seems to reserve some space for thier own checked and unchecked property of an uimenu item. The icon set by using the java command will set the own right behind the reserved space. All my efforts to overwrite the checked/unchecked icon reserved by matlab failed.
Does anyone know how handle this problem?