uimenu buttons remain pressed and trigger also other menus by just sliding over them: pushbutton behaviour desired

StackOverflow https://stackoverflow.com/questions/19423348

Domanda

I implemented various uimenus in my uitable but there appears a very annoying behaviour.

function createUItable

    h = figure
    ...

    uimenu(h,'Label','MenuButton','Callback',@someAction)

end
%---------
function someAction(~,~)
    %some action
end

But after executing the callback function, the menu button remains pressed and highlighted and not even that, when I slide over the next menu button, this one is triggered also!

This behaviour was also described at Matlab Central, but without solution.

I tried the suggested:

function someAction(~,~)
    %some action
    set(gcbo,'Enable','off')
    drawnow
    set(gcbo,'Enable','on')
end

which does not change anything. set(gcbo,'Enable','off') alone would solve the sliding problem, but also disables the whole button, what I don't want. I also tried to use the 'Checked','Visible' and 'Interuptible' property without success. This problem must be known, any hints?

I also thought about using uicontrol instead of uimenu and use a pushbutton, but I don't get it work.


Edit: when I put my menubutton into a submenu it works perfect:

button = uimenu(h,'Label','MenuButton');
uimenu(button,'Label','MenuButton','Callback',@someAction)

Edit2: A pushbutton works also, but how could I place it into the menubar?

È stato utile?

Soluzione

I guess MATLAB implementation is this way, because setting a callback at the top-level menu is very odd. Naturally in GUI's (not only MATLAB), when you click the top-level menu (like "File", "Edit", etc.) the standard behaviour is, that a submenu pops open rather than an immediate action being executed. So you should only use the top-level callback to e.g. dynamically create/modify the associated submenus.

I think there are two alternatives to go:

1) If you'd like to stick to that manner (one, always-visible button-like element), then you should rather use the toolbar via a uipushtool:

hToolbar = uitoolbar(parentFigure);
uipushtool(hToolbar, 'ClickedCallback', @someAction);

This does not have the 'Label' property though, so you'll have to work with 'CData' and may be a 'TooltipString'.

2) Create a top-level menu that contains your actual action-menu:

topMenu = uimenu(parent, 'Label', 'Actions');
uimenu(topMenu, 'Label', 'MenuButton', 'callback', @someAction)

From the general point of view on GUI design, both alternatives have the benefit of being the more commonly used style, thus being more intuitive to any user.

Altri suggerimenti

I found an interesting work-around for this problem while keeping the callback in the TOP menu. Turns out that using the uistack function released focus from the menu item so in the top-level menu callback, I placed

uistack(hObj,'down'); 
uistack(hObj,'up'); 
drawnow; 

Which does nothing to the actual ordering but releases the menu items' focus.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top