Deselect uimenus after click in GUI

Dear all,
I am facing a GUI issue. In the following example, if you click on a menu and slide to the other one, the second one is triggered. I don't want this behaviour. Could someone guide me toward a solution?
function mwe(varargin)
handles.main = figure;
set(handles.main,'Units','pixels','Position',[300 300 210 50],...
'DockControls','Off','MenuBar','none','Toolbar','none');
handles.menu1 = uimenu('Parent',handles.main,'Label','Menu1');
handles.menu2 = uimenu('Parent',handles.main,'Label','Menu2');
handles.txt = uicontrol(handles.main,'Style','text','Units','pixels',...
'String','','HorizontalAlignment','Center','Position',[5 5 200 40]);
set(handles.menu1,'CallBack',{@fcn_txt,handles});
set(handles.menu2,'CallBack',{@fcn_txt,handles});
end
function fcn_txt(hObject,eventdata,handles)
set(handles.txt,'String',get(hObject,'Label'));
end
I tried different solutions and the only one working at the moment is to put a waitbar. I even tried using the setArmed Java method.
Thank you for your help, Cédric

댓글 수: 4

Jan
Jan 2013년 2월 27일
편집: Jan 2013년 2월 27일
What does "to put a waitbar" exactly mean? And what is the "setArmed Java method"?
You explain, what you don't want. But what do you want instead? Perhaps it would be a solution to convince you, that the standard behavior of menus is ok?
The standard behaviour of menus is ok if you have submenus. If your menus are triggering a function, you might want that, after the function, the menu do not stay selected (ie stay in blue).
The setArmed Java method should exactly be what I want. Unfortunately, it did not when I programmed it.
Below is the not-that-clean workaround that I found after asking this question.
function mwe(varargin)
handles.main = figure;
set(handles.main,'Units','pixels','Position',[300 300 210 50],...
'DockControls','Off','MenuBar','none','Toolbar','none');
handles.menu1 = uimenu('Parent',handles.main,'Label','Menu1');
handles.menu2 = uimenu('Parent',handles.main,'Label','Menu2');
handles.txt = uicontrol(handles.main,'Style','text','Units','pixels',...
'String','','HorizontalAlignment','Center','Position',[5 5 200 40]);
set(handles.menu1,'CallBack',{@fcn_txt,handles});
set(handles.menu2,'CallBack',{@fcn_txt,handles});
end
function fcn_txt(hObject,eventdata,handles)
drawnow
pause(0.1)
robot = java.awt.Robot;
position=get(0,'PointerLocation');
set(0,'PointerLocation',[0 0])
robot.mousePress (java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
set(0,'PointerLocation',position)
drawnow
set(handles.txt,'String',get(hObject,'Label'));
end
Thank you for your help.
Sean de Wolski
Sean de Wolski 2013년 2월 28일
편집: Sean de Wolski 2013년 2월 28일
So just to be crystal clear you want the focus to be restored to the figure after?
Cédric Devivier
Cédric Devivier 2013년 2월 28일
I need the uimenu to lose the focus. It may be going somewhere else the figure for instance. I tried adding simply figure(gcf) but it did not work.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2013년 2월 28일

0 개 추천

There might be an equivalent effect for UICONTROLs. Here even the focus remains at the UICONTROL, such that e.g. the KeyPress fcuntion of the figure is not triggered anymore. In this case a solution is:
set(ObjectH, 'Enable', 'off');
drawnow;
set(ObjectH, 'Enable', 'on');
This is more compact than letting the java.robot click to an arbitrary position - which is a very bold action I'd avoid under all circumstances.

댓글 수: 3

Cédric Devivier
Cédric Devivier 2013년 2월 28일
Unfortunately, it is still behaving the same way. So far, the java.robot click is the only solution. Why don't you advise such behaviour?
Jan
Jan 2013년 2월 28일
there could be a GUI with a dangerous button at the location [0,0], e.g. the confirmation dialog of a disk formatting. Or it could be another menu element of the same GUI.
Cédric Devivier
Cédric Devivier 2013년 2월 28일
Thank you, I changed it to click on the same figure.

댓글을 달려면 로그인하십시오.

Jeffrey Girard
Jeffrey Girard 2015년 2월 26일
편집: Jeffrey Girard 2015년 2월 26일

0 개 추천

Unfortunately, Jan's solution didn't work for me either. These aren't ideal solutions, but I have found that this behavior can be avoided by:
  1. Adding a submenu with even one choice
  2. Producing some sort of figure or dialog box with the menu callback (a "success" msgbox)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

질문:

2013년 2월 27일

편집:

2015년 2월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by