Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Uimenu Icon

Subject: Uimenu Icon

From: Dani

Date: 14 May, 2010 16:20:00

Message: 1 of 7

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

Subject: Uimenu Icon

From: Walter Roberson

Date: 14 May, 2010 16:26:04

Message: 2 of 7

Dani wrote:

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

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.

Subject: Uimenu Icon

From: Dani

Date: 17 May, 2010 18:07:08

Message: 3 of 7

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


On May 14, 6:26 pm, Walter Roberson <rober...@hushmail.com> wrote:
> Dani wrote:
> > 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?
>
> 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.

Subject: Uimenu Icon

From: Yair Altman

Date: 18 May, 2010 00:10:08

Message: 4 of 7

Dani <dani.schmid@gmail.com> wrote in message <f04126c1-e45d-4908-a987-296942512e41@37g2000yqm.googlegroups.com>...
> 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

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

Subject: Uimenu Icon

From: Matt Fig

Date: 18 May, 2010 01:34:04

Message: 5 of 7

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.

Subject: Uimenu Icon

From: Yair Altman

Date: 18 May, 2010 08:45:10

Message: 6 of 7

"Matt Fig" <spamanon@yahoo.com> wrote in message <hssqqc$j1r$1@fred.mathworks.com>...
> 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.

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

Subject: Uimenu Icon

From: Jens

Date: 13 May, 2011 12:56:05

Message: 7 of 7

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?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us