MATLAB Answers

Give Focus to GUI Window

190 views (last 30 days)
Jeff
Jeff on 8 Sep 2011
Commented: Christoph on 31 May 2019
Hi everybody,
I have programmed a function that starts off with a set of instructions on a GUI, and the user clicks a button to indicate they've read the instructions. After they click the button, the Matlab command line interface comes up asking them to type in their name. After they type in their name and hit enter, I want the GUI to pop back into view instead of having to manually bring up the window. Is there some sort of focus property that I can set to bring the focus back to the GUI window?
Jeff

  0 Comments

Sign in to comment.

Accepted Answer

Paulo Silva
Paulo Silva on 8 Sep 2011
figure(FigHandle) %FigHandle is the handle for the GUI figure

  1 Comment

Jeff
Jeff on 8 Sep 2011
figure(hObject) generates the error:
"Requested figure handle in use by another object"
This line of code is executed inside a separate function which is called by a callback function (a button callback function which responds to the user indicating they've read the instructions) in the GUI function. I should note that hObject and handles are both passed into the separate function.

Sign in to comment.

More Answers (3)

Jan
Jan on 8 Sep 2011
Unfortunately "figure(FigHandle)" does not work reliably in opposite to the documentation. E.g. a pressed button can keep the keyboard focus. Therefore I've added this code to all callback of my buttons:
set(ButtonH, 'Enable', 'off');
drawnow;
set(ButtonH, 'Enable', 'on');
Then the figure gets the focus. Another solution is the FEX: WindowAPI, which allows to move the focus also.

  4 Comments

Show 1 older comment
Jan
Jan on 8 Sep 2011
See also: http://www.mathworks.com/matlabcentral/newsreader/view_thread/235825 and http://www.mathworks.com/matlabcentral/answers/555-controlling-background-color-of-selected-uicontrol-popup .
Charles
Charles on 12 May 2016
Jan:
5 years later, and this helped me. Thanks!
Jenny Lu
Jenny Lu on 3 Aug 2018
Thank you! A solution to my problems for quite some time.

Sign in to comment.


Jeff
Jeff on 8 Sep 2011
figure(FigHandle) was the correct approach, where FigHandle is the title of your figure.

  0 Comments

Sign in to comment.


claudio
claudio on 26 Apr 2018
You can access directly to java handle and set focus request
% warning off MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame
jFig = get(hFig,'JavaFrame');
jFig.requestFocus
In this way you have not to force enable property of your uicontrols

  2 Comments

Mohammad Askari
Mohammad Askari on 6 Jun 2018
Superb! This is much faster than using drawnow. Thanks.
Christoph
Christoph on 31 May 2019
With 2017b this does not work for me, furthermore I get this warning:
Warning: figure JavaFrame property will be obsoleted in a future release. For more information see the JavaFrame resource on the MathWorks web site.
In my case
figure(FigHandle)
did the job.

Sign in to comment.

Sign in to answer this question.

Tags