Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Uimenu Icon

30 views
Skip to first unread message

Dani

unread,
May 14, 2010, 12:20:00 PM5/14/10
to
Hi,

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

Walter Roberson

unread,
May 14, 2010, 12:26:04 PM5/14/10
to
Dani wrote:

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.

Dani

unread,
May 17, 2010, 2:07:08 PM5/17/10
to
Hi,

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

Yair Altman

unread,
May 17, 2010, 8:10:08 PM5/17/10
to
Dani <dani....@gmail.com> wrote in message <f04126c1-e45d-4908...@37g2000yqm.googlegroups.com>...

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

Matt Fig

unread,
May 17, 2010, 9:34:04 PM5/17/10
to
Yair, I tried to run your example and it failed with the error:

??? 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.

Yair Altman

unread,
May 18, 2010, 4:45:10 AM5/18/10
to
"Matt Fig" <spam...@yahoo.com> wrote in message <hssqqc$j1r$1...@fred.mathworks.com>...

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

Jens

unread,
May 13, 2011, 8:56:05 AM5/13/11
to
Hello,

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?

kamalakar

unread,
Jul 20, 2017, 5:05:23 AM7/20/17
to
"Yair Altman" wrote in message <hstk2m$309$1...@fred.mathworks.com>...
Hi Yair,
I tried this example when I want to run the code as an m-file I get the error
java.lang.ArrayIndexOutOfBoundsException: No such child: 4

at java.awt.Container.getComponent(Unknown Source)

at javax.swing.JMenu.getMenuComponent(Unknown Source)
but if I run the code line by line in command prompt I am not facing this issue
0 new messages