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:
uicontextmenu and listbox

Subject: uicontextmenu and listbox

From: Johnathan

Date: 17 Jun, 2009 00:20:19

Message: 1 of 8

Can you put a uicontextmenu with a listbox or other uicontrol?
I have tried to but nothing happens when I right click.. I am not given an error, though. Doesn't work for a pushbutton either.
Could someone take a look at my code and tell me where I have gone astray?
I am using R2007a.

thanks,
--John


figure
MyListbox=uicontrol('Style','listbox',...
    'String',{'dog1','dog2','dog3','dog4','dog5'},...
    'Position',[10,200,100,100]);

ContextMenu=uicontextmenu;
Menu1=uimenu('Parent',ContextMenu,...
    'Label','Menu Option 1');
Menu2=uimenu('Parent',ContextMenu,...
    'Label','Menu Option 2');
Menu3=uimenu('Parent',ContextMenu,...
    'Label','Menu Option 3');
set(MyListbox,'UIContextMenu',ContextMenu);
get(MyListbox,'UIContextMenu')

Subject: uicontextmenu and listbox

From: Matt Fig

Date: 17 Jun, 2009 01:43:01

Message: 2 of 8

I am using 2007a on Win Vista 32bit and it works for me.

Subject: uicontextmenu and listbox

From: Johnathan

Date: 17 Jun, 2009 01:50:17

Message: 3 of 8

weird. I am using it on Mac OSX. I wonder if it is not supported for mac OS.
The only time I can get uicontext menu to work is with axes.

Does anyone else know about this issue and how I can get around it? I would like to use a context menu, but I am worried now that they won't display for some users, and I would hate to build some important functions dependent on the uicontext menu and have them not work. I can't really test them at all if I can't see the context menu anyway, though.

This is interesting.
Thanks
--John

Subject: uicontextmenu and listbox

From: Matt Fig

Date: 17 Jun, 2009 04:56:01

Message: 4 of 8

Can you see the contextmenu if you do this?

set(ContextMenu,'visible','on')

I seem to remember there was a bug with the visible property of uicontextmenus. Maybe a search the online bug reports would be productive.

Subject: uicontextmenu and listbox

From: Johnathan

Date: 17 Jun, 2009 05:50:16

Message: 5 of 8

Thanks for the suggestion Matt.
I searched r2007a for uicontextmenu
From the bug report:
_________________________________________
Summary

The Visible property of uicontextmenu may not work correctly.
Description

If you set the Visible property of a context menu (uicontextmenu) to on programmatically, the context menu may not display. Also, if you get the value of the Visible property of the context menu, the returned value may be off even if the context menu is visible on the screen.
Workaround

Introduce a drawnow command after the creation of the context menu and before setting its Visible property to on.
Exists in Release (Version)

Exists in R2007a(7.4), R2006b(7.3), R2006a(7.2), R14SP3(7.1), R14SP2(7.0.4), R14SP1(7.0.1), R14(7.0)
_________________________________________

I tried the workaround that they suggested, but it doesn't seem to help.
I did try installing my copy of matlab on my windows partition, and the context menu works fine. Has anyone else encountered this issue in Mac OS and gotten it to work?

Thanks
--John

Subject: uicontextmenu and listbox

From: Johnathan

Date: 17 Jun, 2009 06:09:01

Message: 6 of 8

I put a few more set(ContextMenu,'Visible','on') and drawnows in there and the uicontextmenu now shows up right when the figure is created past the bottom the figure, but as soon as you click anywhere on the screen it disappears and won't come back.

get(ContextMenu,'Visible') returns 'off' no matter what.

--John

Subject: uicontextmenu and listbox

From: Yair Altman

Date: 15 Aug, 2009 19:18:03

Message: 7 of 8

"Johnathan " <durchfalldurchfall@yahoo.com> wrote in message <h1a19t$5mg$1@fred.mathworks.com>...
> I put a few more set(ContextMenu,'Visible','on') and drawnows in there and the uicontextmenu now shows up right when the figure is created past the bottom the figure, but as soon as you click anywhere on the screen it disappears and won't come back.
>
> get(ContextMenu,'Visible') returns 'off' no matter what.
>
> --John


Even if the Matlab context-menu displays as requested, it is still static, meaning that it doesn't change based on the actual listbox item that is right-clicked. Yesterday I posted a solution to this problem: http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/

Yair Altman
http://UndocumentedMatlab.com
 

Subject: uicontextmenu and listbox

From: Michelle Tadmor

Date: 3 Apr, 2012 20:23:22

Message: 8 of 8

>
> I tried the workaround that they suggested, but it doesn't seem to help.
> I did try installing my copy of matlab on my windows partition, and the context menu works fine. Has anyone else encountered this issue in Mac OS and gotten it to work?
>
> Thanks
> --John

Hey yea, I'm just placing a 'get' requesting the context menu handle on every myList_callback and that seems to fix the problem:


function lstGates_Callback(hObject, ~, ~)
    
    % -ignore- matlab right click menu not showing bug workaround
    ctm = get(hObject, 'UIContextMenu');

    ....
    ....
end

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